Skip to content

Commit

Permalink
Auto merge of rust-lang#10798 - Alexendoo:unnameable-ty-suggestions, …
Browse files Browse the repository at this point in the history
…r=Jarcho

Don't suggest unnameable types in `box_default`, `let_underscore_untyped`

changelog: [`box_default`], [`let_underscore_untyped`]: Don't suggest unnameable types
  • Loading branch information
bors committed May 18, 2023
2 parents 22b3196 + cfa5aa2 commit 392e955
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
9 changes: 6 additions & 3 deletions clippy_lints/src/box_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use rustc_hir::{
Block, Expr, ExprKind, Local, Node, QPath, TyKind,
};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::{lint::in_external_macro, ty::print::with_forced_trimmed_paths};
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::print::with_forced_trimmed_paths;
use rustc_middle::ty::IsSuggestable;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::sym;

Expand Down Expand Up @@ -49,7 +51,6 @@ impl LateLintPass<'_> for BoxDefault {
&& path_def_id(cx, ty).map_or(false, |id| Some(id) == cx.tcx.lang_items().owned_box())
&& is_default_equivalent(cx, arg)
{
let arg_ty = cx.typeck_results().expr_ty(arg);
span_lint_and_sugg(
cx,
BOX_DEFAULT,
Expand All @@ -58,8 +59,10 @@ impl LateLintPass<'_> for BoxDefault {
"try",
if is_plain_default(arg_path) || given_type(cx, expr) {
"Box::default()".into()
} else {
} else if let Some(arg_ty) = cx.typeck_results().expr_ty(arg).make_suggestable(cx.tcx, true) {
with_forced_trimmed_paths!(format!("Box::<{arg_ty}>::default()"))
} else {
return
},
Applicability::MachineApplicable
);
Expand Down
17 changes: 8 additions & 9 deletions clippy_lints/src/let_underscore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::is_from_proc_macro;
use clippy_utils::ty::{implements_trait, is_must_use_ty, match_type};
use clippy_utils::{is_must_use_func_call, paths};
use rustc_hir::{ExprKind, Local, PatKind};
use rustc_hir::{Local, PatKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::subst::GenericArgKind;
use rustc_middle::ty::IsSuggestable;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{BytePos, Span};

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

// Ignore function calls that return impl traits...
if let Some(init) = local.init &&
matches!(init.kind, ExprKind::Call(_, _) | ExprKind::MethodCall(_, _, _, _)) {
let expr_ty = cx.typeck_results().expr_ty(init);
if expr_ty.is_impl_trait() {
return;
}
}
// Ignore unnameable types
if let Some(init) = local.init
&& !cx.typeck_results().expr_ty(init).is_suggestable(cx.tcx, true)
{
return;
}

// Ignore if it is from a procedural macro...
if is_from_proc_macro(cx, init) {
Expand Down
9 changes: 8 additions & 1 deletion tests/ui/box_default.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@run-rustfix
#![warn(clippy::box_default)]
#![allow(clippy::default_constructed_unit_structs)]
#![allow(unused, clippy::default_constructed_unit_structs)]

#[derive(Default)]
struct ImplementsDefault;
Expand Down Expand Up @@ -35,6 +35,13 @@ fn main() {
let _more = ret_ty_fn();
call_ty_fn(Box::default());
issue_10381();

// `Box::<Option<_>>::default()` would be valid here, but not `Box::default()` or
// `Box::<Option<[closure@...]>::default()`
//
// Would have a suggestion after https://github.com/rust-lang/rust/blob/fdd030127cc68afec44a8d3f6341525dd34e50ae/compiler/rustc_middle/src/ty/diagnostics.rs#L554-L563
let mut unnameable = Box::new(Option::default());
let _ = unnameable.insert(|| {});
}

fn ret_ty_fn() -> Box<bool> {
Expand Down
9 changes: 8 additions & 1 deletion tests/ui/box_default.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@run-rustfix
#![warn(clippy::box_default)]
#![allow(clippy::default_constructed_unit_structs)]
#![allow(unused, clippy::default_constructed_unit_structs)]

#[derive(Default)]
struct ImplementsDefault;
Expand Down Expand Up @@ -35,6 +35,13 @@ fn main() {
let _more = ret_ty_fn();
call_ty_fn(Box::new(u8::default()));
issue_10381();

// `Box::<Option<_>>::default()` would be valid here, but not `Box::default()` or
// `Box::<Option<[closure@...]>::default()`
//
// Would have a suggestion after https://github.com/rust-lang/rust/blob/fdd030127cc68afec44a8d3f6341525dd34e50ae/compiler/rustc_middle/src/ty/diagnostics.rs#L554-L563
let mut unnameable = Box::new(Option::default());
let _ = unnameable.insert(|| {});
}

fn ret_ty_fn() -> Box<bool> {
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/box_default.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,25 @@ LL | call_ty_fn(Box::new(u8::default()));
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()`

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

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

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

error: `Box::new(_)` of default value
--> $DIR/box_default.rs:79:18
--> $DIR/box_default.rs:86:18
|
LL | Some(Box::new(Foo::default()))
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<Foo>::default()`
Expand Down
1 change: 1 addition & 0 deletions tests/ui/let_underscore_untyped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn main() {
let _ = e();
let _ = f();
let _ = g();
let closure = || {};

_ = a();
_ = b(1);
Expand Down

0 comments on commit 392e955

Please sign in to comment.