Skip to content

Commit 5d41179

Browse files
committed
Reorganize conditionals: Run faster checks first
1 parent 267f41b commit 5d41179

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,32 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
7878
span: Span,
7979
node_id: NodeId
8080
) {
81+
// Perform some preliminary checks that rule out constness on the Clippy side. This way we
82+
// can skip the actual const check and return early.
83+
match kind {
84+
FnKind::ItemFn(name, _generics, header, _vis, attrs) => {
85+
if !can_be_const_fn(&name.as_str(), header, attrs) {
86+
return;
87+
}
88+
},
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) {
93+
return;
94+
}
95+
},
96+
_ => return
97+
}
98+
8199
let def_id = cx.tcx.hir().local_def_id(node_id);
82100
let mir = cx.tcx.optimized_mir(def_id);
83-
if let Err((span, err) = is_min_const_fn(cx.tcx, def_id, &mir) {
84-
cx.tcx.sess.span_err(span, &err);
85-
} else {
86-
match kind {
87-
FnKind::ItemFn(name, _generics, header, _vis, attrs) => {
88-
if !can_be_const_fn(&name.as_str(), header, attrs) {
89-
return;
90-
}
91-
},
92-
FnKind::Method(ident, sig, _vis, attrs) => {
93-
let header = sig.header;
94-
let name = ident.name.as_str();
95-
if !can_be_const_fn(&name, header, attrs) {
96-
return;
97-
}
98-
},
99-
_ => return
101+
102+
if let Err((span, err)) = is_min_const_fn(cx.tcx, def_id, &mir) {
103+
if cx.tcx.is_min_const_fn(def_id) {
104+
cx.tcx.sess.span_err(span, &err);
100105
}
106+
} else {
101107
span_lint(cx, MISSING_CONST_FOR_FN, span, "this could be a const_fn");
102108
}
103109
}

tests/ui/missing_const_for_fn/cant_be_const.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ fn main() {
4141

4242
trait Foo {
4343
// This should not be suggested to be made const
44-
// (rustc restriction)
44+
// (rustc doesn't allow const trait methods)
4545
fn f() -> u32;
46+
47+
// This should not be suggested to be made const either
48+
fn g() -> u32 {
49+
33
50+
}
4651
}
4752

4853
// Don't lint custom entrypoints either

tests/ui/missing_const_for_fn/could_be_const.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ error: this could be a const_fn
1616
LL | fn one() -> i32 { 1 }
1717
| ^^^^^^^^^^^^^^^^^^^^^
1818

19+
error: this could be a const_fn
20+
--> $DIR/could_be_const.rs:23:1
21+
|
22+
LL | / fn two() -> i32 {
23+
LL | | let abc = 2;
24+
LL | | abc
25+
LL | | }
26+
| |_^
27+
1928
error: this could be a const_fn
2029
--> $DIR/could_be_const.rs:30:1
2130
|
@@ -38,5 +47,5 @@ LL | | t
3847
LL | | }
3948
| |_^
4049

41-
error: aborting due to 5 previous errors
50+
error: aborting due to 6 previous errors
4251

0 commit comments

Comments
 (0)