mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 13:13:34 +00:00
cargo fmt
This commit is contained in:
parent
f9d65b6356
commit
c0a02691d8
5 changed files with 54 additions and 40 deletions
|
@ -1,13 +1,13 @@
|
|||
use rustc::hir;
|
||||
use rustc::hir::{Body, FnDecl, Constness};
|
||||
use rustc::hir::intravisit::FnKind;
|
||||
use rustc::hir::{Body, Constness, FnDecl};
|
||||
// use rustc::mir::*;
|
||||
use syntax::ast::{NodeId, Attribute};
|
||||
use syntax_pos::Span;
|
||||
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 crate::utils::{span_lint, is_entrypoint_fn};
|
||||
use syntax::ast::{Attribute, NodeId};
|
||||
use syntax_pos::Span;
|
||||
|
||||
/// **What it does:**
|
||||
///
|
||||
|
@ -26,8 +26,12 @@ use crate::utils::{span_lint, is_entrypoint_fn};
|
|||
/// Also, the lint only runs one pass over the code. Consider these two non-const functions:
|
||||
///
|
||||
/// ```rust
|
||||
/// fn a() -> i32 { 0 }
|
||||
/// fn b() -> i32 { a() }
|
||||
/// fn a() -> i32 {
|
||||
/// 0
|
||||
/// }
|
||||
/// fn b() -> i32 {
|
||||
/// a()
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// When running Clippy, the lint will only suggest to make `a` const, because `b` at this time
|
||||
|
@ -38,9 +42,7 @@ use crate::utils::{span_lint, is_entrypoint_fn};
|
|||
///
|
||||
/// ```rust
|
||||
/// fn new() -> Self {
|
||||
/// Self {
|
||||
/// random_number: 42
|
||||
/// }
|
||||
/// Self { random_number: 42 }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
|
@ -48,9 +50,7 @@ use crate::utils::{span_lint, is_entrypoint_fn};
|
|||
///
|
||||
/// ```rust
|
||||
/// const fn new() -> Self {
|
||||
/// Self {
|
||||
/// random_number: 42
|
||||
/// }
|
||||
/// Self { random_number: 42 }
|
||||
/// }
|
||||
/// ```
|
||||
declare_clippy_lint! {
|
||||
|
@ -80,7 +80,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
|
|||
_: &FnDecl,
|
||||
_: &Body,
|
||||
span: Span,
|
||||
node_id: NodeId
|
||||
node_id: NodeId,
|
||||
) {
|
||||
// Perform some preliminary checks that rule out constness on the Clippy side. This way we
|
||||
// can skip the actual const check and return early.
|
||||
|
@ -97,7 +97,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
|
|||
return;
|
||||
}
|
||||
},
|
||||
_ => return
|
||||
_ => return,
|
||||
}
|
||||
|
||||
let def_id = cx.tcx.hir().local_def_id(node_id);
|
||||
|
@ -115,9 +115,13 @@ 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 }
|
||||
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 }
|
||||
if header.constness == Constness::Const {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
|
|
@ -354,11 +354,9 @@ pub fn method_chain_args<'a>(expr: &'a Expr, methods: &[&str]) -> Option<Vec<&'a
|
|||
///
|
||||
/// 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"
|
||||
});
|
||||
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"
|
||||
}
|
||||
|
|
|
@ -8,16 +8,22 @@
|
|||
struct Game;
|
||||
|
||||
// This should not be linted because it's already const
|
||||
const fn already_const() -> i32 { 32 }
|
||||
const fn already_const() -> i32 {
|
||||
32
|
||||
}
|
||||
|
||||
impl Game {
|
||||
// This should not be linted because it's already const
|
||||
pub const fn already_const() -> i32 { 32 }
|
||||
pub const fn already_const() -> i32 {
|
||||
32
|
||||
}
|
||||
}
|
||||
|
||||
// Allowing on this function, because it would lint, which we don't want in this case.
|
||||
#[allow(clippy::missing_const_for_fn)]
|
||||
fn random() -> u32 { 42 }
|
||||
fn random() -> u32 {
|
||||
42
|
||||
}
|
||||
|
||||
// We should not suggest to make this function `const` because `random()` is non-const
|
||||
fn random_caller() -> u32 {
|
||||
|
@ -30,7 +36,7 @@ static Y: u32 = 0;
|
|||
// refer to a static variable
|
||||
fn get_y() -> u32 {
|
||||
Y
|
||||
//~^ ERROR E0013
|
||||
//~^ ERROR E0013
|
||||
}
|
||||
|
||||
// Also main should not be suggested to be made const
|
||||
|
@ -52,4 +58,6 @@ trait Foo {
|
|||
|
||||
// Don't lint custom entrypoints either
|
||||
#[start]
|
||||
fn init(num: isize, something: *const *const u8) -> isize { 1 }
|
||||
fn init(num: isize, something: *const *const u8) -> isize {
|
||||
1
|
||||
}
|
||||
|
|
|
@ -10,14 +10,14 @@ struct Game {
|
|||
impl Game {
|
||||
// Could be const
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
guess: 42,
|
||||
}
|
||||
Self { guess: 42 }
|
||||
}
|
||||
}
|
||||
|
||||
// Could be const
|
||||
fn one() -> i32 { 1 }
|
||||
fn one() -> i32 {
|
||||
1
|
||||
}
|
||||
|
||||
// Could also be const
|
||||
fn two() -> i32 {
|
||||
|
@ -32,7 +32,9 @@ fn string() -> String {
|
|||
}
|
||||
|
||||
// Could be const
|
||||
unsafe fn four() -> i32 { 4 }
|
||||
unsafe fn four() -> i32 {
|
||||
4
|
||||
}
|
||||
|
||||
// Could also be const
|
||||
fn generic<T>(t: T) -> T {
|
||||
|
|
|
@ -2,19 +2,19 @@ error: this could be a const_fn
|
|||
--> $DIR/could_be_const.rs:12:5
|
||||
|
|
||||
LL | / pub fn new() -> Self {
|
||||
LL | | Self {
|
||||
LL | | guess: 42,
|
||||
LL | | }
|
||||
LL | | Self { guess: 42 }
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
|
||||
= note: `-D clippy::missing-const-for-fn` implied by `-D warnings`
|
||||
|
||||
error: this could be a const_fn
|
||||
--> $DIR/could_be_const.rs:20:1
|
||||
--> $DIR/could_be_const.rs:18:1
|
||||
|
|
||||
LL | fn one() -> i32 { 1 }
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
LL | / fn one() -> i32 {
|
||||
LL | | 1
|
||||
LL | | }
|
||||
| |_^
|
||||
|
||||
error: this could be a const_fn
|
||||
--> $DIR/could_be_const.rs:23:1
|
||||
|
@ -36,11 +36,13 @@ LL | | }
|
|||
error: this could be a const_fn
|
||||
--> $DIR/could_be_const.rs:35:1
|
||||
|
|
||||
LL | unsafe fn four() -> i32 { 4 }
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
LL | / unsafe fn four() -> i32 {
|
||||
LL | | 4
|
||||
LL | | }
|
||||
| |_^
|
||||
|
||||
error: this could be a const_fn
|
||||
--> $DIR/could_be_const.rs:38:1
|
||||
--> $DIR/could_be_const.rs:40:1
|
||||
|
|
||||
LL | / fn generic<T>(t: T) -> T {
|
||||
LL | | t
|
||||
|
|
Loading…
Reference in a new issue