diff --git a/clippy_lints/src/missing_const_for_fn.rs b/clippy_lints/src/missing_const_for_fn.rs index 4e85d13332c..2afde0c9315 100644 --- a/clippy_lints/src/missing_const_for_fn.rs +++ b/clippy_lints/src/missing_const_for_fn.rs @@ -6,7 +6,7 @@ use crate::utils::{is_entrypoint_fn, span_lint}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; use rustc_mir::transform::qualify_min_const_fn::is_min_const_fn; -use syntax::ast::{Attribute, NodeId}; +use syntax::ast::NodeId; use syntax_pos::Span; /// **What it does:** @@ -82,25 +82,28 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn { span: Span, node_id: NodeId, ) { + let def_id = cx.tcx.hir().local_def_id(node_id); + + if is_entrypoint_fn(cx, def_id) { + return; + } + // Perform some preliminary checks that rule out constness on the Clippy side. This way we // can skip the actual const check and return early. match kind { - FnKind::ItemFn(name, _generics, header, _vis, attrs) => { - if !can_be_const_fn(&name.as_str(), header, attrs) { + FnKind::ItemFn(_, _, header, ..) => { + if already_const(header) { return; } }, - FnKind::Method(ident, sig, _vis, attrs) => { - let header = sig.header; - let name = ident.name.as_str(); - if !can_be_const_fn(&name, header, attrs) { + FnKind::Method(_, sig, ..) => { + if already_const(sig.header) { return; } }, _ => return, } - let def_id = cx.tcx.hir().local_def_id(node_id); let mir = cx.tcx.optimized_mir(def_id); if let Err((span, err)) = is_min_const_fn(cx.tcx, def_id, &mir) { @@ -113,15 +116,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn { } } -fn can_be_const_fn(name: &str, header: hir::FnHeader, attrs: &[Attribute]) -> bool { - // Main and custom entrypoints can't be `const` - if is_entrypoint_fn(name, attrs) { - return false; - } - - // We don't have to lint on something that's already `const` - if header.constness == Constness::Const { - return false; - } - true +// We don't have to lint on something that's already `const` +fn already_const(header: hir::FnHeader) -> bool { + header.constness == Constness::Const } diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 32fe5e22dd5..8ce28bfed1f 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -3,7 +3,7 @@ use if_chain::if_chain; use matches::matches; use rustc::hir; use rustc::hir::def::Def; -use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX}; +use rustc::hir::def_id::{DefId, LOCAL_CRATE, CRATE_DEF_INDEX}; use rustc::hir::intravisit::{NestedVisitorMap, Visitor}; use rustc::hir::Node; use rustc::hir::*; @@ -350,15 +350,12 @@ pub fn method_chain_args<'a>(expr: &'a Expr, methods: &[&str]) -> Option bool { - let is_custom_entrypoint = attrs - .iter() - .any(|attr| attr.path.segments.len() == 1 && attr.path.segments[0].ident.to_string() == "start"); - - is_custom_entrypoint || fn_name == "main" +/// Returns true if the provided `def_id` is an entrypoint to a program +pub fn is_entrypoint_fn(cx: &LateContext<'_, '_>, def_id: DefId) -> bool { + if let Some((entry_fn_def_id, _)) = cx.tcx.entry_fn(LOCAL_CRATE) { + return def_id == entry_fn_def_id + } + false } /// Get the name of the item the expression is in, if available. diff --git a/tests/ui/missing_const_for_fn/cant_be_const.rs b/tests/ui/missing_const_for_fn/cant_be_const.rs index ede3724cc6b..36efe16b84f 100644 --- a/tests/ui/missing_const_for_fn/cant_be_const.rs +++ b/tests/ui/missing_const_for_fn/cant_be_const.rs @@ -39,10 +39,10 @@ fn get_y() -> u32 { //~^ ERROR E0013 } -// Also main should not be suggested to be made const -fn main() { - // We should also be sure to not lint on closures - let add_one_v2 = |x: u32| -> u32 { x + 1 }; +// Don't lint entrypoint functions +#[start] +fn init(num: isize, something: *const *const u8) -> isize { + 1 } trait Foo { @@ -55,9 +55,3 @@ trait Foo { 33 } } - -// Don't lint custom entrypoints either -#[start] -fn init(num: isize, something: *const *const u8) -> isize { - 1 -}