Skip to content

Commit 0c6bdda

Browse files
committed
Use built-in entry_fn detection over self-built
1 parent c0a0269 commit 0c6bdda

File tree

3 files changed

+25
-39
lines changed

3 files changed

+25
-39
lines changed

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::utils::{is_entrypoint_fn, span_lint};
66
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
77
use rustc::{declare_tool_lint, lint_array};
88
use rustc_mir::transform::qualify_min_const_fn::is_min_const_fn;
9-
use syntax::ast::{Attribute, NodeId};
9+
use syntax::ast::NodeId;
1010
use syntax_pos::Span;
1111

1212
/// **What it does:**
@@ -82,25 +82,28 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
8282
span: Span,
8383
node_id: NodeId,
8484
) {
85+
let def_id = cx.tcx.hir().local_def_id(node_id);
86+
87+
if is_entrypoint_fn(cx, def_id) {
88+
return;
89+
}
90+
8591
// Perform some preliminary checks that rule out constness on the Clippy side. This way we
8692
// can skip the actual const check and return early.
8793
match kind {
88-
FnKind::ItemFn(name, _generics, header, _vis, attrs) => {
89-
if !can_be_const_fn(&name.as_str(), header, attrs) {
94+
FnKind::ItemFn(_, _, header, ..) => {
95+
if already_const(header) {
9096
return;
9197
}
9298
},
93-
FnKind::Method(ident, sig, _vis, attrs) => {
94-
let header = sig.header;
95-
let name = ident.name.as_str();
96-
if !can_be_const_fn(&name, header, attrs) {
99+
FnKind::Method(_, sig, ..) => {
100+
if already_const(sig.header) {
97101
return;
98102
}
99103
},
100104
_ => return,
101105
}
102106

103-
let def_id = cx.tcx.hir().local_def_id(node_id);
104107
let mir = cx.tcx.optimized_mir(def_id);
105108

106109
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 {
113116
}
114117
}
115118

116-
fn can_be_const_fn(name: &str, header: hir::FnHeader, attrs: &[Attribute]) -> bool {
117-
// Main and custom entrypoints can't be `const`
118-
if is_entrypoint_fn(name, attrs) {
119-
return false;
120-
}
121-
122-
// We don't have to lint on something that's already `const`
123-
if header.constness == Constness::Const {
124-
return false;
125-
}
126-
true
119+
// We don't have to lint on something that's already `const`
120+
fn already_const(header: hir::FnHeader) -> bool {
121+
header.constness == Constness::Const
127122
}

clippy_lints/src/utils/mod.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use if_chain::if_chain;
33
use matches::matches;
44
use rustc::hir;
55
use rustc::hir::def::Def;
6-
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
6+
use rustc::hir::def_id::{DefId, LOCAL_CRATE, CRATE_DEF_INDEX};
77
use rustc::hir::intravisit::{NestedVisitorMap, Visitor};
88
use rustc::hir::Node;
99
use rustc::hir::*;
@@ -350,15 +350,12 @@ pub fn method_chain_args<'a>(expr: &'a Expr, methods: &[&str]) -> Option<Vec<&'a
350350
Some(matched)
351351
}
352352

353-
/// Returns true if the function is an entrypoint to a program
354-
///
355-
/// This is either the usual `main` function or a custom function with the `#[start]` attribute.
356-
pub fn is_entrypoint_fn(fn_name: &str, attrs: &[ast::Attribute]) -> bool {
357-
let is_custom_entrypoint = attrs
358-
.iter()
359-
.any(|attr| attr.path.segments.len() == 1 && attr.path.segments[0].ident.to_string() == "start");
360-
361-
is_custom_entrypoint || fn_name == "main"
353+
/// Returns true if the provided `def_id` is an entrypoint to a program
354+
pub fn is_entrypoint_fn(cx: &LateContext<'_, '_>, def_id: DefId) -> bool {
355+
if let Some((entry_fn_def_id, _)) = cx.tcx.entry_fn(LOCAL_CRATE) {
356+
return def_id == entry_fn_def_id
357+
}
358+
false
362359
}
363360

364361
/// Get the name of the item the expression is in, if available.

tests/ui/missing_const_for_fn/cant_be_const.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ fn get_y() -> u32 {
3939
//~^ ERROR E0013
4040
}
4141

42-
// Also main should not be suggested to be made const
43-
fn main() {
44-
// We should also be sure to not lint on closures
45-
let add_one_v2 = |x: u32| -> u32 { x + 1 };
42+
// Don't lint entrypoint functions
43+
#[start]
44+
fn init(num: isize, something: *const *const u8) -> isize {
45+
1
4646
}
4747

4848
trait Foo {
@@ -55,9 +55,3 @@ trait Foo {
5555
33
5656
}
5757
}
58-
59-
// Don't lint custom entrypoints either
60-
#[start]
61-
fn init(num: isize, something: *const *const u8) -> isize {
62-
1
63-
}

0 commit comments

Comments
 (0)