Skip to content

Commit 5dab1c7

Browse files
committed
Use built-in entry_fn detection over self-built
1 parent d2053f3 commit 5dab1c7

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:**
@@ -78,25 +78,28 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
7878
span: Span,
7979
node_id: NodeId,
8080
) {
81+
let def_id = cx.tcx.hir().local_def_id(node_id);
82+
83+
if is_entrypoint_fn(cx, def_id) {
84+
return;
85+
}
86+
8187
// Perform some preliminary checks that rule out constness on the Clippy side. This way we
8288
// can skip the actual const check and return early.
8389
match kind {
84-
FnKind::ItemFn(name, _generics, header, _vis, attrs) => {
85-
if !can_be_const_fn(&name.as_str(), header, attrs) {
90+
FnKind::ItemFn(_, _, header, ..) => {
91+
if already_const(header) {
8692
return;
8793
}
8894
},
89-
FnKind::Method(ident, sig, _vis, attrs) => {
90-
let header = sig.header;
91-
let name = ident.name.as_str();
92-
if !can_be_const_fn(&name, header, attrs) {
95+
FnKind::Method(_, sig, ..) => {
96+
if already_const(sig.header) {
9397
return;
9498
}
9599
},
96100
_ => return,
97101
}
98102

99-
let def_id = cx.tcx.hir().local_def_id(node_id);
100103
let mir = cx.tcx.optimized_mir(def_id);
101104

102105
if let Err((span, err)) = is_min_const_fn(cx.tcx, def_id, &mir) {
@@ -109,15 +112,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
109112
}
110113
}
111114

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

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::*;
@@ -346,15 +346,12 @@ pub fn method_chain_args<'a>(expr: &'a Expr, methods: &[&str]) -> Option<Vec<&'a
346346
Some(matched)
347347
}
348348

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

360357
/// 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)