Skip to content

Commit cadac5b

Browse files
committed
cargo fmt
1 parent 5d41179 commit cadac5b

File tree

5 files changed

+54
-40
lines changed

5 files changed

+54
-40
lines changed

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use rustc::hir;
2-
use rustc::hir::{Body, FnDecl, Constness};
32
use rustc::hir::intravisit::FnKind;
3+
use rustc::hir::{Body, Constness, FnDecl};
44
// use rustc::mir::*;
5-
use syntax::ast::{NodeId, Attribute};
6-
use syntax_pos::Span;
5+
use crate::utils::{is_entrypoint_fn, span_lint};
76
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
87
use rustc::{declare_tool_lint, lint_array};
98
use rustc_mir::transform::qualify_min_const_fn::is_min_const_fn;
10-
use crate::utils::{span_lint, is_entrypoint_fn};
9+
use syntax::ast::{Attribute, NodeId};
10+
use syntax_pos::Span;
1111

1212
/// **What it does:**
1313
///
@@ -26,8 +26,12 @@ use crate::utils::{span_lint, is_entrypoint_fn};
2626
/// Also, the lint only runs one pass over the code. Consider these two non-const functions:
2727
///
2828
/// ```rust
29-
/// fn a() -> i32 { 0 }
30-
/// fn b() -> i32 { a() }
29+
/// fn a() -> i32 {
30+
/// 0
31+
/// }
32+
/// fn b() -> i32 {
33+
/// a()
34+
/// }
3135
/// ```
3236
///
3337
/// When running Clippy, the lint will only suggest to make `a` const, because `b` at this time
@@ -38,19 +42,15 @@ use crate::utils::{span_lint, is_entrypoint_fn};
3842
///
3943
/// ```rust
4044
/// fn new() -> Self {
41-
/// Self {
42-
/// random_number: 42
43-
/// }
45+
/// Self { random_number: 42 }
4446
/// }
4547
/// ```
4648
///
4749
/// Could be a const fn:
4850
///
4951
/// ```rust
5052
/// const fn new() -> Self {
51-
/// Self {
52-
/// random_number: 42
53-
/// }
53+
/// Self { random_number: 42 }
5454
/// }
5555
/// ```
5656
declare_clippy_lint! {
@@ -76,7 +76,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
7676
_: &FnDecl,
7777
_: &Body,
7878
span: Span,
79-
node_id: NodeId
79+
node_id: NodeId,
8080
) {
8181
// Perform some preliminary checks that rule out constness on the Clippy side. This way we
8282
// can skip the actual const check and return early.
@@ -93,7 +93,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
9393
return;
9494
}
9595
},
96-
_ => return
96+
_ => return,
9797
}
9898

9999
let def_id = cx.tcx.hir().local_def_id(node_id);
@@ -111,9 +111,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
111111

112112
fn can_be_const_fn(name: &str, header: hir::FnHeader, attrs: &[Attribute]) -> bool {
113113
// Main and custom entrypoints can't be `const`
114-
if is_entrypoint_fn(name, attrs) { return false }
114+
if is_entrypoint_fn(name, attrs) {
115+
return false;
116+
}
115117

116118
// We don't have to lint on something that's already `const`
117-
if header.constness == Constness::Const { return false }
119+
if header.constness == Constness::Const {
120+
return false;
121+
}
118122
true
119123
}

clippy_lints/src/utils/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,9 @@ pub fn method_chain_args<'a>(expr: &'a Expr, methods: &[&str]) -> Option<Vec<&'a
334334
///
335335
/// This is either the usual `main` function or a custom function with the `#[start]` attribute.
336336
pub fn is_entrypoint_fn(fn_name: &str, attrs: &[ast::Attribute]) -> bool {
337-
338-
let is_custom_entrypoint = attrs.iter().any(|attr| {
339-
attr.path.segments.len() == 1
340-
&& attr.path.segments[0].ident.to_string() == "start"
341-
});
337+
let is_custom_entrypoint = attrs
338+
.iter()
339+
.any(|attr| attr.path.segments.len() == 1 && attr.path.segments[0].ident.to_string() == "start");
342340

343341
is_custom_entrypoint || fn_name == "main"
344342
}

tests/ui/missing_const_for_fn/cant_be_const.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@
88
struct Game;
99

1010
// This should not be linted because it's already const
11-
const fn already_const() -> i32 { 32 }
11+
const fn already_const() -> i32 {
12+
32
13+
}
1214

1315
impl Game {
1416
// This should not be linted because it's already const
15-
pub const fn already_const() -> i32 { 32 }
17+
pub const fn already_const() -> i32 {
18+
32
19+
}
1620
}
1721

1822
// Allowing on this function, because it would lint, which we don't want in this case.
1923
#[allow(clippy::missing_const_for_fn)]
20-
fn random() -> u32 { 42 }
24+
fn random() -> u32 {
25+
42
26+
}
2127

2228
// We should not suggest to make this function `const` because `random()` is non-const
2329
fn random_caller() -> u32 {
@@ -30,7 +36,7 @@ static Y: u32 = 0;
3036
// refer to a static variable
3137
fn get_y() -> u32 {
3238
Y
33-
//~^ ERROR E0013
39+
//~^ ERROR E0013
3440
}
3541

3642
// Also main should not be suggested to be made const
@@ -52,4 +58,6 @@ trait Foo {
5258

5359
// Don't lint custom entrypoints either
5460
#[start]
55-
fn init(num: isize, something: *const *const u8) -> isize { 1 }
61+
fn init(num: isize, something: *const *const u8) -> isize {
62+
1
63+
}

tests/ui/missing_const_for_fn/could_be_const.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ struct Game {
1010
impl Game {
1111
// Could be const
1212
pub fn new() -> Self {
13-
Self {
14-
guess: 42,
15-
}
13+
Self { guess: 42 }
1614
}
1715
}
1816

1917
// Could be const
20-
fn one() -> i32 { 1 }
18+
fn one() -> i32 {
19+
1
20+
}
2121

2222
// Could also be const
2323
fn two() -> i32 {
@@ -32,7 +32,9 @@ fn string() -> String {
3232
}
3333

3434
// Could be const
35-
unsafe fn four() -> i32 { 4 }
35+
unsafe fn four() -> i32 {
36+
4
37+
}
3638

3739
// Could also be const
3840
fn generic<T>(t: T) -> T {

tests/ui/missing_const_for_fn/could_be_const.stderr

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ error: this could be a const_fn
22
--> $DIR/could_be_const.rs:12:5
33
|
44
LL | / pub fn new() -> Self {
5-
LL | | Self {
6-
LL | | guess: 42,
7-
LL | | }
5+
LL | | Self { guess: 42 }
86
LL | | }
97
| |_____^
108
|
119
= note: `-D clippy::missing-const-for-fn` implied by `-D warnings`
1210

1311
error: this could be a const_fn
14-
--> $DIR/could_be_const.rs:20:1
12+
--> $DIR/could_be_const.rs:18:1
1513
|
16-
LL | fn one() -> i32 { 1 }
17-
| ^^^^^^^^^^^^^^^^^^^^^
14+
LL | / fn one() -> i32 {
15+
LL | | 1
16+
LL | | }
17+
| |_^
1818

1919
error: this could be a const_fn
2020
--> $DIR/could_be_const.rs:23:1
@@ -36,11 +36,13 @@ LL | | }
3636
error: this could be a const_fn
3737
--> $DIR/could_be_const.rs:35:1
3838
|
39-
LL | unsafe fn four() -> i32 { 4 }
40-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
LL | / unsafe fn four() -> i32 {
40+
LL | | 4
41+
LL | | }
42+
| |_^
4143

4244
error: this could be a const_fn
43-
--> $DIR/could_be_const.rs:38:1
45+
--> $DIR/could_be_const.rs:40:1
4446
|
4547
LL | / fn generic<T>(t: T) -> T {
4648
LL | | t

0 commit comments

Comments
 (0)