Skip to content

Commit f9d65b6

Browse files
committed
Reorganize conditionals: Run faster checks first
1 parent 68cc4df commit f9d65b6

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

clippy_lints/src/missing_const_for_fn.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,32 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
8282
span: Span,
8383
node_id: NodeId
8484
) {
85+
// Perform some preliminary checks that rule out constness on the Clippy side. This way we
86+
// can skip the actual const check and return early.
87+
match kind {
88+
FnKind::ItemFn(name, _generics, header, _vis, attrs) => {
89+
if !can_be_const_fn(&name.as_str(), header, attrs) {
90+
return;
91+
}
92+
},
93+
FnKind::Method(ident, sig, _vis, attrs) => {
94+
let header = sig.header;
95+
let name = ident.name.as_str();
96+
if !can_be_const_fn(&name, header, attrs) {
97+
return;
98+
}
99+
},
100+
_ => return
101+
}
102+
85103
let def_id = cx.tcx.hir().local_def_id(node_id);
86104
let mir = cx.tcx.optimized_mir(def_id);
87-
if let Err((span, err) = is_min_const_fn(cx.tcx, def_id, &mir) {
88-
cx.tcx.sess.span_err(span, &err);
89-
} else {
90-
match kind {
91-
FnKind::ItemFn(name, _generics, header, _vis, attrs) => {
92-
if !can_be_const_fn(&name.as_str(), header, attrs) {
93-
return;
94-
}
95-
},
96-
FnKind::Method(ident, sig, _vis, attrs) => {
97-
let header = sig.header;
98-
let name = ident.name.as_str();
99-
if !can_be_const_fn(&name, header, attrs) {
100-
return;
101-
}
102-
},
103-
_ => return
105+
106+
if let Err((span, err)) = is_min_const_fn(cx.tcx, def_id, &mir) {
107+
if cx.tcx.is_min_const_fn(def_id) {
108+
cx.tcx.sess.span_err(span, &err);
104109
}
110+
} else {
105111
span_lint(cx, MISSING_CONST_FOR_FN, span, "this could be a const_fn");
106112
}
107113
}

tests/ui/missing_const_for_fn/cant_be_const.rs

+6-1
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

+10-1
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)