Skip to content

Commit f7e2cb4

Browse files
committed
Auto merge of rust-lang#9308 - daxpedda:missing-const-for-fn, r=Jarcho
Use `check_proc_macro` for `missing_const_for_fn` This uses `@Jarcho's` rust-lang#8694 implementation to fix `missing_const_for_fn` linting in proc-macros. I'm not 100% sure what I'm doing here, any feedback is appreciated. Previously: Jarcho/rust-clippy#1. Fixes rust-lang#8854. changelog: [`missing_const_for_fn`]: No longer lints in proc-macros
2 parents 3af9072 + fd60581 commit f7e2cb4

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-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

+35-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, Node, QPath, TraitItem,
19+
TraitItemKind, UnOp, UnsafeSource, Unsafety, Variant, VariantData, YieldSource,
2020
};
2121
use rustc_lint::{LateContext, LintContext};
2222
use rustc_middle::ty::TyCtxt;
@@ -250,6 +250,26 @@ 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) = match kind {
255+
FnKind::ItemFn(.., header) => (fn_header_search_pat(*header), Pat::Str("")),
256+
FnKind::Method(.., sig) => (fn_header_search_pat(sig.header), Pat::Str("")),
257+
FnKind::Closure => return (Pat::Str(""), expr_search_pat(tcx, &body.value).1),
258+
};
259+
let start_pat = match tcx.hir().get(hir_id) {
260+
Node::Item(Item { vis_span, .. }) | Node::ImplItem(ImplItem { vis_span, .. }) => {
261+
if vis_span.is_empty() {
262+
start_pat
263+
} else {
264+
Pat::Str("pub")
265+
}
266+
},
267+
Node::TraitItem(_) => start_pat,
268+
_ => Pat::Str(""),
269+
};
270+
(start_pat, end_pat)
271+
}
272+
253273
pub trait WithSearchPat {
254274
type Context: LintContext;
255275
fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat);
@@ -277,6 +297,18 @@ impl_with_search_pat!(LateContext: ImplItem with impl_item_search_pat);
277297
impl_with_search_pat!(LateContext: FieldDef with field_def_search_pat);
278298
impl_with_search_pat!(LateContext: Variant with variant_search_pat);
279299

300+
impl<'cx> WithSearchPat for (&FnKind<'cx>, &Body<'cx>, HirId, Span) {
301+
type Context = LateContext<'cx>;
302+
303+
fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat) {
304+
fn_kind_pat(cx.tcx, self.0, self.1, self.2)
305+
}
306+
307+
fn span(&self) -> Span {
308+
self.3
309+
}
310+
}
311+
280312
/// Checks if the item likely came from a proc-macro.
281313
///
282314
/// 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)