Skip to content

Commit 8f8099f

Browse files
committed
Account for const stability in clippy when checking constness
1 parent c330150 commit 8f8099f

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

src/tools/clippy/clippy_utils/src/visitors.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use crate::msrvs::Msrv;
12
use crate::ty::needs_ordered_drop;
23
use crate::{get_enclosing_block, path_to_local_id};
4+
use crate::qualify_min_const_fn::is_stable_const_fn;
35
use core::ops::ControlFlow;
46
use rustc_ast::visit::{VisitorResult, try_visit};
57
use rustc_hir::def::{CtorKind, DefKind, Res};
@@ -343,13 +345,13 @@ pub fn is_const_evaluatable<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) ->
343345
.cx
344346
.qpath_res(p, hir_id)
345347
.opt_def_id()
346-
.is_some_and(|id| self.cx.tcx.is_const_fn(id)) => {},
348+
.is_some_and(|id| is_stable_const_fn(self.cx, id, Msrv::default())) => {},
347349
ExprKind::MethodCall(..)
348350
if self
349351
.cx
350352
.typeck_results()
351353
.type_dependent_def_id(e.hir_id)
352-
.is_some_and(|id| self.cx.tcx.is_const_fn(id)) => {},
354+
.is_some_and(|id| is_stable_const_fn(self.cx, id, Msrv::default())) => {},
353355
ExprKind::Binary(_, lhs, rhs)
354356
if self.cx.typeck_results().expr_ty(lhs).peel_refs().is_primitive_ty()
355357
&& self.cx.typeck_results().expr_ty(rhs).peel_refs().is_primitive_ty() => {},

src/tools/clippy/tests/ui/or_fun_call.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,8 @@ fn fn_call_in_nested_expr() {
406406
val: String::from("123"),
407407
});
408408

409-
// ok, `String::default()` is now `const`
410-
let _ = opt_foo.unwrap_or(Foo { val: String::default() });
409+
let _ = opt_foo.unwrap_or_else(|| Foo { val: String::default() });
410+
//~^ or_fun_call
411411
}
412412

413413
mod result_map_or {

src/tools/clippy/tests/ui/or_fun_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,8 @@ fn fn_call_in_nested_expr() {
406406
val: String::from("123"),
407407
});
408408

409-
// ok, `String::default()` is now `const`
410409
let _ = opt_foo.unwrap_or(Foo { val: String::default() });
410+
//~^ or_fun_call
411411
}
412412

413413
mod result_map_or {

src/tools/clippy/tests/ui/or_fun_call.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ error: use of `unwrap_or` to construct default value
240240
LL | let _ = opt.unwrap_or({ i32::default() });
241241
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
242242

243+
error: function call inside of `unwrap_or`
244+
--> tests/ui/or_fun_call.rs:409:21
245+
|
246+
LL | let _ = opt_foo.unwrap_or(Foo { val: String::default() });
247+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| Foo { val: String::default() })`
248+
243249
error: function call inside of `map_or`
244250
--> tests/ui/or_fun_call.rs:424:19
245251
|
@@ -258,5 +264,5 @@ error: function call inside of `get_or_insert`
258264
LL | let _ = x.get_or_insert(g());
259265
| ^^^^^^^^^^^^^^^^^^ help: try: `get_or_insert_with(g)`
260266

261-
error: aborting due to 40 previous errors
267+
error: aborting due to 41 previous errors
262268

0 commit comments

Comments
 (0)