Use built-in entry_fn detection over self-built

This commit is contained in:
Philipp Hansch 2019-01-22 07:36:15 +01:00
parent c0a02691d8
commit 0c6bdda562
No known key found for this signature in database
GPG Key ID: B6FA06A6E0E2665B
3 changed files with 25 additions and 39 deletions

View File

@ -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
}

View File

@ -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<Vec<&'a
Some(matched)
}
/// Returns true if the function is an entrypoint to a program
///
/// This is either the usual `main` function or a custom function with the `#[start]` attribute.
pub fn is_entrypoint_fn(fn_name: &str, attrs: &[ast::Attribute]) -> 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.

View File

@ -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
}