Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
phansch committed Jan 21, 2019
1 parent 5d41179 commit cadac5b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 40 deletions.
36 changes: 20 additions & 16 deletions clippy_lints/src/missing_const_for_fn.rs
Original file line number Diff line number Diff line change
@@ -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:**
///
Expand All @@ -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
Expand All @@ -38,19 +42,15 @@ use crate::utils::{span_lint, is_entrypoint_fn};
///
/// ```rust
/// fn new() -> Self {
/// Self {
/// random_number: 42
/// }
/// Self { random_number: 42 }
/// }
/// ```
///
/// Could be a const fn:
///
/// ```rust
/// const fn new() -> Self {
/// Self {
/// random_number: 42
/// }
/// Self { random_number: 42 }
/// }
/// ```
declare_clippy_lint! {
Expand All @@ -76,7 +76,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.
Expand All @@ -93,7 +93,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
return;
}
},
_ => return
_ => return,
}

let def_id = cx.tcx.hir().local_def_id(node_id);
Expand All @@ -111,9 +111,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
}
8 changes: 3 additions & 5 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,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"
}
Expand Down
18 changes: 13 additions & 5 deletions tests/ui/missing_const_for_fn/cant_be_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
}
12 changes: 7 additions & 5 deletions tests/ui/missing_const_for_fn/could_be_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
20 changes: 11 additions & 9 deletions tests/ui/missing_const_for_fn/could_be_const.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit cadac5b

Please sign in to comment.