From c3301503b97cd6b768a944661b81a92994e9db00 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Sat, 21 Dec 2024 20:25:43 +0000 Subject: Make `Default` const and add some `const Default` impls Full list of `impl const Default` types: - () - bool - char - Cell - std::ascii::Char - usize - u8 - u16 - u32 - u64 - u128 - i8 - i16 - i32 - i64 - i128 - f16 - f32 - f64 - f128 - std::marker::PhantomData - Option - std::iter::Empty - std::ptr::Alignment - &[T] - &mut [T] - &str - &mut str - String - Vec --- src/tools/clippy/tests/ui/or_fun_call.fixed | 4 ++-- src/tools/clippy/tests/ui/or_fun_call.rs | 2 +- src/tools/clippy/tests/ui/or_fun_call.stderr | 8 +------- 3 files changed, 4 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/tools/clippy/tests/ui/or_fun_call.fixed b/src/tools/clippy/tests/ui/or_fun_call.fixed index 34f3e046841..38822bdbb30 100644 --- a/src/tools/clippy/tests/ui/or_fun_call.fixed +++ b/src/tools/clippy/tests/ui/or_fun_call.fixed @@ -406,8 +406,8 @@ fn fn_call_in_nested_expr() { val: String::from("123"), }); - let _ = opt_foo.unwrap_or_else(|| Foo { val: String::default() }); - //~^ or_fun_call + // ok, `String::default()` is now `const` + let _ = opt_foo.unwrap_or(Foo { val: String::default() }); } mod result_map_or { diff --git a/src/tools/clippy/tests/ui/or_fun_call.rs b/src/tools/clippy/tests/ui/or_fun_call.rs index dc57bd6060a..7e8647f221c 100644 --- a/src/tools/clippy/tests/ui/or_fun_call.rs +++ b/src/tools/clippy/tests/ui/or_fun_call.rs @@ -406,8 +406,8 @@ fn fn_call_in_nested_expr() { val: String::from("123"), }); + // ok, `String::default()` is now `const` let _ = opt_foo.unwrap_or(Foo { val: String::default() }); - //~^ or_fun_call } mod result_map_or { diff --git a/src/tools/clippy/tests/ui/or_fun_call.stderr b/src/tools/clippy/tests/ui/or_fun_call.stderr index 0f159fe8bff..0d88929e131 100644 --- a/src/tools/clippy/tests/ui/or_fun_call.stderr +++ b/src/tools/clippy/tests/ui/or_fun_call.stderr @@ -240,12 +240,6 @@ error: use of `unwrap_or` to construct default value LL | let _ = opt.unwrap_or({ i32::default() }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()` -error: function call inside of `unwrap_or` - --> tests/ui/or_fun_call.rs:409:21 - | -LL | let _ = opt_foo.unwrap_or(Foo { val: String::default() }); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| Foo { val: String::default() })` - error: function call inside of `map_or` --> tests/ui/or_fun_call.rs:424:19 | @@ -264,5 +258,5 @@ error: function call inside of `get_or_insert` LL | let _ = x.get_or_insert(g()); | ^^^^^^^^^^^^^^^^^^ help: try: `get_or_insert_with(g)` -error: aborting due to 41 previous errors +error: aborting due to 40 previous errors -- cgit 1.4.1-3-g733a5 From 8f8099fb42f0b067cd9b6a82e704ce3cc0e63301 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Mon, 7 Jul 2025 23:07:32 +0000 Subject: Account for const stability in clippy when checking constness --- src/tools/clippy/clippy_utils/src/visitors.rs | 6 ++++-- src/tools/clippy/tests/ui/or_fun_call.fixed | 4 ++-- src/tools/clippy/tests/ui/or_fun_call.rs | 2 +- src/tools/clippy/tests/ui/or_fun_call.stderr | 8 +++++++- 4 files changed, 14 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/tools/clippy/clippy_utils/src/visitors.rs b/src/tools/clippy/clippy_utils/src/visitors.rs index fc6e30a9804..615a0910dfd 100644 --- a/src/tools/clippy/clippy_utils/src/visitors.rs +++ b/src/tools/clippy/clippy_utils/src/visitors.rs @@ -1,5 +1,7 @@ +use crate::msrvs::Msrv; use crate::ty::needs_ordered_drop; use crate::{get_enclosing_block, path_to_local_id}; +use crate::qualify_min_const_fn::is_stable_const_fn; use core::ops::ControlFlow; use rustc_ast::visit::{VisitorResult, try_visit}; use rustc_hir::def::{CtorKind, DefKind, Res}; @@ -343,13 +345,13 @@ pub fn is_const_evaluatable<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> .cx .qpath_res(p, hir_id) .opt_def_id() - .is_some_and(|id| self.cx.tcx.is_const_fn(id)) => {}, + .is_some_and(|id| is_stable_const_fn(self.cx, id, Msrv::default())) => {}, ExprKind::MethodCall(..) if self .cx .typeck_results() .type_dependent_def_id(e.hir_id) - .is_some_and(|id| self.cx.tcx.is_const_fn(id)) => {}, + .is_some_and(|id| is_stable_const_fn(self.cx, id, Msrv::default())) => {}, ExprKind::Binary(_, lhs, rhs) if self.cx.typeck_results().expr_ty(lhs).peel_refs().is_primitive_ty() && self.cx.typeck_results().expr_ty(rhs).peel_refs().is_primitive_ty() => {}, diff --git a/src/tools/clippy/tests/ui/or_fun_call.fixed b/src/tools/clippy/tests/ui/or_fun_call.fixed index 38822bdbb30..34f3e046841 100644 --- a/src/tools/clippy/tests/ui/or_fun_call.fixed +++ b/src/tools/clippy/tests/ui/or_fun_call.fixed @@ -406,8 +406,8 @@ fn fn_call_in_nested_expr() { val: String::from("123"), }); - // ok, `String::default()` is now `const` - let _ = opt_foo.unwrap_or(Foo { val: String::default() }); + let _ = opt_foo.unwrap_or_else(|| Foo { val: String::default() }); + //~^ or_fun_call } mod result_map_or { diff --git a/src/tools/clippy/tests/ui/or_fun_call.rs b/src/tools/clippy/tests/ui/or_fun_call.rs index 7e8647f221c..dc57bd6060a 100644 --- a/src/tools/clippy/tests/ui/or_fun_call.rs +++ b/src/tools/clippy/tests/ui/or_fun_call.rs @@ -406,8 +406,8 @@ fn fn_call_in_nested_expr() { val: String::from("123"), }); - // ok, `String::default()` is now `const` let _ = opt_foo.unwrap_or(Foo { val: String::default() }); + //~^ or_fun_call } mod result_map_or { diff --git a/src/tools/clippy/tests/ui/or_fun_call.stderr b/src/tools/clippy/tests/ui/or_fun_call.stderr index 0d88929e131..0f159fe8bff 100644 --- a/src/tools/clippy/tests/ui/or_fun_call.stderr +++ b/src/tools/clippy/tests/ui/or_fun_call.stderr @@ -240,6 +240,12 @@ error: use of `unwrap_or` to construct default value LL | let _ = opt.unwrap_or({ i32::default() }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()` +error: function call inside of `unwrap_or` + --> tests/ui/or_fun_call.rs:409:21 + | +LL | let _ = opt_foo.unwrap_or(Foo { val: String::default() }); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| Foo { val: String::default() })` + error: function call inside of `map_or` --> tests/ui/or_fun_call.rs:424:19 | @@ -258,5 +264,5 @@ error: function call inside of `get_or_insert` LL | let _ = x.get_or_insert(g()); | ^^^^^^^^^^^^^^^^^^ help: try: `get_or_insert_with(g)` -error: aborting due to 40 previous errors +error: aborting due to 41 previous errors -- cgit 1.4.1-3-g733a5