Skip to content

Commit fc5e699

Browse files
committed
Use check_proc_macro for missing_const_for_fn
1 parent 3af9072 commit fc5e699

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

clippy_lints/src/missing_const_for_fn.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::qualify_min_const_fn::is_min_const_fn;
33
use clippy_utils::ty::has_drop;
4-
use clippy_utils::{fn_has_unsatisfiable_preds, is_entrypoint_fn, meets_msrv, msrvs, trait_ref_of_method};
4+
use clippy_utils::{
5+
fn_has_unsatisfiable_preds, is_entrypoint_fn, is_from_proc_macro, meets_msrv, msrvs, trait_ref_of_method,
6+
};
57
use rustc_hir as hir;
68
use rustc_hir::def_id::CRATE_DEF_ID;
79
use rustc_hir::intravisit::FnKind;
@@ -86,10 +88,10 @@ impl MissingConstForFn {
8688
impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
8789
fn check_fn(
8890
&mut self,
89-
cx: &LateContext<'_>,
90-
kind: FnKind<'_>,
91+
cx: &LateContext<'tcx>,
92+
kind: FnKind<'tcx>,
9193
_: &FnDecl<'_>,
92-
_: &Body<'_>,
94+
body: &Body<'tcx>,
9395
span: Span,
9496
hir_id: HirId,
9597
) {
@@ -144,6 +146,10 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
144146
}
145147
}
146148

149+
if is_from_proc_macro(cx, &(&kind, body, hir_id, span)) {
150+
return;
151+
}
152+
147153
let mir = cx.tcx.optimized_mir(def_id);
148154

149155
if let Err((span, err)) = is_min_const_fn(cx.tcx, mir, self.msrv) {

clippy_utils/src/check_proc_macro.rs

+36-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
1515
use rustc_ast::ast::{IntTy, LitIntType, LitKind, StrStyle, UintTy};
1616
use rustc_hir::{
17-
Block, BlockCheckMode, Closure, Destination, Expr, ExprKind, FieldDef, FnHeader, Impl, ImplItem, ImplItemKind,
18-
IsAuto, Item, ItemKind, LoopSource, MatchSource, QPath, TraitItem, TraitItemKind, UnOp, UnsafeSource, Unsafety,
19-
Variant, VariantData, YieldSource,
17+
intravisit::FnKind, Block, BlockCheckMode, Body, Closure, Destination, Expr, ExprKind, FieldDef, FnHeader, HirId,
18+
Impl, ImplItem, ImplItemKind, IsAuto, Item, ItemKind, LoopSource, MatchSource, QPath, TraitItem, TraitItemKind,
19+
UnOp, UnsafeSource, Unsafety, Variant, VariantData, YieldSource,
2020
};
2121
use rustc_lint::{LateContext, LintContext};
2222
use rustc_middle::ty::TyCtxt;
@@ -250,6 +250,27 @@ fn variant_search_pat(v: &Variant<'_>) -> (Pat, Pat) {
250250
}
251251
}
252252

253+
fn fn_kind_pat(tcx: TyCtxt<'_>, kind: &FnKind<'_>, body: &Body<'_>, hir_id: HirId) -> (Pat, Pat) {
254+
let (start_pat, end_pat, visibility) = match kind {
255+
FnKind::ItemFn(.., header) => (
256+
fn_header_search_pat(*header),
257+
Pat::Str(""),
258+
tcx.visibility(tcx.hir().local_def_id(hir_id)),
259+
),
260+
FnKind::Method(.., sig) => (
261+
fn_header_search_pat(sig.header),
262+
Pat::Str(""),
263+
tcx.visibility(tcx.hir().local_def_id(hir_id)),
264+
),
265+
FnKind::Closure => return (Pat::Str(""), expr_search_pat(tcx, &body.value).1),
266+
};
267+
if visibility.is_public() {
268+
(Pat::Str("pub"), end_pat)
269+
} else {
270+
(start_pat, end_pat)
271+
}
272+
}
273+
253274
pub trait WithSearchPat {
254275
type Context: LintContext;
255276
fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat);
@@ -277,6 +298,18 @@ impl_with_search_pat!(LateContext: ImplItem with impl_item_search_pat);
277298
impl_with_search_pat!(LateContext: FieldDef with field_def_search_pat);
278299
impl_with_search_pat!(LateContext: Variant with variant_search_pat);
279300

301+
impl<'cx> WithSearchPat for (&FnKind<'cx>, &Body<'cx>, HirId, Span) {
302+
type Context = LateContext<'cx>;
303+
304+
fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat) {
305+
fn_kind_pat(cx.tcx, &self.0, &self.1, self.2)
306+
}
307+
308+
fn span(&self) -> Span {
309+
self.3
310+
}
311+
}
312+
280313
/// Checks if the item likely came from a proc-macro.
281314
///
282315
/// This should be called after `in_external_macro` and the initial pattern matching of the ast as

tests/ui/missing_const_for_fn/cant_be_const.rs

+9
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
//! The .stderr output of this test should be empty. Otherwise it's a bug somewhere.
44
55
// aux-build:helper.rs
6+
// aux-build:../../auxiliary/proc_macro_with_span.rs
67

78
#![warn(clippy::missing_const_for_fn)]
89
#![feature(start)]
910
#![feature(custom_inner_attributes)]
1011

1112
extern crate helper;
13+
extern crate proc_macro_with_span;
14+
15+
use proc_macro_with_span::with_span;
1216

1317
struct Game;
1418

@@ -119,3 +123,8 @@ mod const_fn_stabilized_after_msrv {
119123
byte.is_ascii_digit();
120124
}
121125
}
126+
127+
with_span! {
128+
span
129+
fn dont_check_in_proc_macro() {}
130+
}

0 commit comments

Comments
 (0)