Skip to content

Commit 392e955

Browse files
committed
Auto merge of #10798 - Alexendoo:unnameable-ty-suggestions, r=Jarcho
Don't suggest unnameable types in `box_default`, `let_underscore_untyped` changelog: [`box_default`], [`let_underscore_untyped`]: Don't suggest unnameable types
2 parents 22b3196 + cfa5aa2 commit 392e955

File tree

6 files changed

+35
-18
lines changed

6 files changed

+35
-18
lines changed

Diff for: clippy_lints/src/box_default.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use rustc_hir::{
88
Block, Expr, ExprKind, Local, Node, QPath, TyKind,
99
};
1010
use rustc_lint::{LateContext, LateLintPass, LintContext};
11-
use rustc_middle::{lint::in_external_macro, ty::print::with_forced_trimmed_paths};
11+
use rustc_middle::lint::in_external_macro;
12+
use rustc_middle::ty::print::with_forced_trimmed_paths;
13+
use rustc_middle::ty::IsSuggestable;
1214
use rustc_session::{declare_lint_pass, declare_tool_lint};
1315
use rustc_span::sym;
1416

@@ -49,7 +51,6 @@ impl LateLintPass<'_> for BoxDefault {
4951
&& path_def_id(cx, ty).map_or(false, |id| Some(id) == cx.tcx.lang_items().owned_box())
5052
&& is_default_equivalent(cx, arg)
5153
{
52-
let arg_ty = cx.typeck_results().expr_ty(arg);
5354
span_lint_and_sugg(
5455
cx,
5556
BOX_DEFAULT,
@@ -58,8 +59,10 @@ impl LateLintPass<'_> for BoxDefault {
5859
"try",
5960
if is_plain_default(arg_path) || given_type(cx, expr) {
6061
"Box::default()".into()
61-
} else {
62+
} else if let Some(arg_ty) = cx.typeck_results().expr_ty(arg).make_suggestable(cx.tcx, true) {
6263
with_forced_trimmed_paths!(format!("Box::<{arg_ty}>::default()"))
64+
} else {
65+
return
6366
},
6467
Applicability::MachineApplicable
6568
);

Diff for: clippy_lints/src/let_underscore.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::is_from_proc_macro;
33
use clippy_utils::ty::{implements_trait, is_must_use_ty, match_type};
44
use clippy_utils::{is_must_use_func_call, paths};
5-
use rustc_hir::{ExprKind, Local, PatKind};
5+
use rustc_hir::{Local, PatKind};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_middle::lint::in_external_macro;
88
use rustc_middle::ty::subst::GenericArgKind;
9+
use rustc_middle::ty::IsSuggestable;
910
use rustc_session::{declare_lint_pass, declare_tool_lint};
1011
use rustc_span::{BytePos, Span};
1112

@@ -192,14 +193,12 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
192193
if local.pat.default_binding_modes && local.ty.is_none() {
193194
// When `default_binding_modes` is true, the `let` keyword is present.
194195

195-
// Ignore function calls that return impl traits...
196-
if let Some(init) = local.init &&
197-
matches!(init.kind, ExprKind::Call(_, _) | ExprKind::MethodCall(_, _, _, _)) {
198-
let expr_ty = cx.typeck_results().expr_ty(init);
199-
if expr_ty.is_impl_trait() {
200-
return;
201-
}
202-
}
196+
// Ignore unnameable types
197+
if let Some(init) = local.init
198+
&& !cx.typeck_results().expr_ty(init).is_suggestable(cx.tcx, true)
199+
{
200+
return;
201+
}
203202

204203
// Ignore if it is from a procedural macro...
205204
if is_from_proc_macro(cx, init) {

Diff for: tests/ui/box_default.fixed

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@run-rustfix
22
#![warn(clippy::box_default)]
3-
#![allow(clippy::default_constructed_unit_structs)]
3+
#![allow(unused, clippy::default_constructed_unit_structs)]
44

55
#[derive(Default)]
66
struct ImplementsDefault;
@@ -35,6 +35,13 @@ fn main() {
3535
let _more = ret_ty_fn();
3636
call_ty_fn(Box::default());
3737
issue_10381();
38+
39+
// `Box::<Option<_>>::default()` would be valid here, but not `Box::default()` or
40+
// `Box::<Option<[closure@...]>::default()`
41+
//
42+
// Would have a suggestion after https://github.com/rust-lang/rust/blob/fdd030127cc68afec44a8d3f6341525dd34e50ae/compiler/rustc_middle/src/ty/diagnostics.rs#L554-L563
43+
let mut unnameable = Box::new(Option::default());
44+
let _ = unnameable.insert(|| {});
3845
}
3946

4047
fn ret_ty_fn() -> Box<bool> {

Diff for: tests/ui/box_default.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@run-rustfix
22
#![warn(clippy::box_default)]
3-
#![allow(clippy::default_constructed_unit_structs)]
3+
#![allow(unused, clippy::default_constructed_unit_structs)]
44

55
#[derive(Default)]
66
struct ImplementsDefault;
@@ -35,6 +35,13 @@ fn main() {
3535
let _more = ret_ty_fn();
3636
call_ty_fn(Box::new(u8::default()));
3737
issue_10381();
38+
39+
// `Box::<Option<_>>::default()` would be valid here, but not `Box::default()` or
40+
// `Box::<Option<[closure@...]>::default()`
41+
//
42+
// Would have a suggestion after https://github.com/rust-lang/rust/blob/fdd030127cc68afec44a8d3f6341525dd34e50ae/compiler/rustc_middle/src/ty/diagnostics.rs#L554-L563
43+
let mut unnameable = Box::new(Option::default());
44+
let _ = unnameable.insert(|| {});
3845
}
3946

4047
fn ret_ty_fn() -> Box<bool> {

Diff for: tests/ui/box_default.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,25 @@ LL | call_ty_fn(Box::new(u8::default()));
7373
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()`
7474

7575
error: `Box::new(_)` of default value
76-
--> $DIR/box_default.rs:41:5
76+
--> $DIR/box_default.rs:48:5
7777
|
7878
LL | Box::new(bool::default())
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<bool>::default()`
8080

8181
error: `Box::new(_)` of default value
82-
--> $DIR/box_default.rs:58:28
82+
--> $DIR/box_default.rs:65:28
8383
|
8484
LL | let _: Box<dyn Read> = Box::new(ImplementsDefault::default());
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<ImplementsDefault>::default()`
8686

8787
error: `Box::new(_)` of default value
88-
--> $DIR/box_default.rs:67:17
88+
--> $DIR/box_default.rs:74:17
8989
|
9090
LL | let _ = Box::new(WeirdPathed::default());
9191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<WeirdPathed>::default()`
9292

9393
error: `Box::new(_)` of default value
94-
--> $DIR/box_default.rs:79:18
94+
--> $DIR/box_default.rs:86:18
9595
|
9696
LL | Some(Box::new(Foo::default()))
9797
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<Foo>::default()`

Diff for: tests/ui/let_underscore_untyped.rs

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ fn main() {
5454
let _ = e();
5555
let _ = f();
5656
let _ = g();
57+
let closure = || {};
5758

5859
_ = a();
5960
_ = b(1);

0 commit comments

Comments
 (0)