mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-27 09:14:20 +00:00
Auto merge of #9308 - daxpedda:missing-const-for-fn, r=Jarcho
Use `check_proc_macro` for `missing_const_for_fn` This uses `@Jarcho's` #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: https://github.com/Jarcho/rust-clippy/pull/1. Fixes #8854. changelog: [`missing_const_for_fn`]: No longer lints in proc-macros
This commit is contained in:
commit
f7e2cb4470
@ -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;
|
||||
@ -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,
|
||||
) {
|
||||
@ -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) {
|
||||
|
@ -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;
|
||||
@ -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);
|
||||
@ -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
|
||||
|
@ -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;
|
||||
|
||||
@ -119,3 +123,8 @@ mod const_fn_stabilized_after_msrv {
|
||||
byte.is_ascii_digit();
|
||||
}
|
||||
}
|
||||
|
||||
with_span! {
|
||||
span
|
||||
fn dont_check_in_proc_macro() {}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user