Skip to content

Use check_proc_macro for missing_const_for_fn #9308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions clippy_lints/src/missing_const_for_fn.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::qualify_min_const_fn::is_min_const_fn;
use clippy_utils::ty::has_drop;
use clippy_utils::{fn_has_unsatisfiable_preds, is_entrypoint_fn, meets_msrv, msrvs, trait_ref_of_method};
use clippy_utils::{
fn_has_unsatisfiable_preds, is_entrypoint_fn, is_from_proc_macro, meets_msrv, msrvs, trait_ref_of_method,
};
use rustc_hir as hir;
use rustc_hir::def_id::CRATE_DEF_ID;
use rustc_hir::intravisit::FnKind;
Expand Down Expand Up @@ -86,10 +88,10 @@ impl MissingConstForFn {
impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
fn check_fn(
&mut self,
cx: &LateContext<'_>,
kind: FnKind<'_>,
cx: &LateContext<'tcx>,
kind: FnKind<'tcx>,
_: &FnDecl<'_>,
_: &Body<'_>,
body: &Body<'tcx>,
span: Span,
hir_id: HirId,
) {
Expand Down Expand Up @@ -144,6 +146,10 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
}
}

if is_from_proc_macro(cx, &(&kind, body, hir_id, span)) {
return;
}

let mir = cx.tcx.optimized_mir(def_id);

if let Err((span, err)) = is_min_const_fn(cx.tcx, mir, self.msrv) {
Expand Down
38 changes: 35 additions & 3 deletions clippy_utils/src/check_proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

use rustc_ast::ast::{IntTy, LitIntType, LitKind, StrStyle, UintTy};
use rustc_hir::{
Block, BlockCheckMode, Closure, Destination, Expr, ExprKind, FieldDef, FnHeader, Impl, ImplItem, ImplItemKind,
IsAuto, Item, ItemKind, LoopSource, MatchSource, QPath, TraitItem, TraitItemKind, UnOp, UnsafeSource, Unsafety,
Variant, VariantData, YieldSource,
intravisit::FnKind, Block, BlockCheckMode, Body, Closure, Destination, Expr, ExprKind, FieldDef, FnHeader, HirId,
Impl, ImplItem, ImplItemKind, IsAuto, Item, ItemKind, LoopSource, MatchSource, Node, QPath, TraitItem,
TraitItemKind, UnOp, UnsafeSource, Unsafety, Variant, VariantData, YieldSource,
};
use rustc_lint::{LateContext, LintContext};
use rustc_middle::ty::TyCtxt;
Expand Down Expand Up @@ -250,6 +250,26 @@ fn variant_search_pat(v: &Variant<'_>) -> (Pat, Pat) {
}
}

fn fn_kind_pat(tcx: TyCtxt<'_>, kind: &FnKind<'_>, body: &Body<'_>, hir_id: HirId) -> (Pat, Pat) {
let (start_pat, end_pat) = match kind {
FnKind::ItemFn(.., header) => (fn_header_search_pat(*header), Pat::Str("")),
FnKind::Method(.., sig) => (fn_header_search_pat(sig.header), Pat::Str("")),
FnKind::Closure => return (Pat::Str(""), expr_search_pat(tcx, &body.value).1),
};
let start_pat = match tcx.hir().get(hir_id) {
Node::Item(Item { vis_span, .. }) | Node::ImplItem(ImplItem { vis_span, .. }) => {
if vis_span.is_empty() {
start_pat
} else {
Pat::Str("pub")
}
},
Node::TraitItem(_) => start_pat,
_ => Pat::Str(""),
};
(start_pat, end_pat)
}

pub trait WithSearchPat {
type Context: LintContext;
fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat);
Expand Down Expand Up @@ -277,6 +297,18 @@ impl_with_search_pat!(LateContext: ImplItem with impl_item_search_pat);
impl_with_search_pat!(LateContext: FieldDef with field_def_search_pat);
impl_with_search_pat!(LateContext: Variant with variant_search_pat);

impl<'cx> WithSearchPat for (&FnKind<'cx>, &Body<'cx>, HirId, Span) {
type Context = LateContext<'cx>;

fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat) {
fn_kind_pat(cx.tcx, self.0, self.1, self.2)
}

fn span(&self) -> Span {
self.3
}
}

/// Checks if the item likely came from a proc-macro.
///
/// This should be called after `in_external_macro` and the initial pattern matching of the ast as
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/missing_const_for_fn/cant_be_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
//! The .stderr output of this test should be empty. Otherwise it's a bug somewhere.

// aux-build:helper.rs
// aux-build:../../auxiliary/proc_macro_with_span.rs

#![warn(clippy::missing_const_for_fn)]
#![feature(start)]
#![feature(custom_inner_attributes)]

extern crate helper;
extern crate proc_macro_with_span;

use proc_macro_with_span::with_span;

struct Game;

Expand Down Expand Up @@ -119,3 +123,8 @@ mod const_fn_stabilized_after_msrv {
byte.is_ascii_digit();
}
}

with_span! {
span
fn dont_check_in_proc_macro() {}
}