Skip to content

Commit 745b790

Browse files
committed
Only suggest type in as_underscore when it's suggestable
1 parent 49e2f37 commit 745b790

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

clippy_lints/src/casts/as_underscore.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use rustc_errors::Applicability;
33
use rustc_hir::{Expr, Ty, TyKind};
44
use rustc_lint::LateContext;
5-
use rustc_middle::ty;
5+
use rustc_middle::ty::IsSuggestable;
66

77
use super::AS_UNDERSCORE;
88

99
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, ty: &'tcx Ty<'_>) {
1010
if matches!(ty.kind, TyKind::Infer(())) {
1111
span_lint_and_then(cx, AS_UNDERSCORE, expr.span, "using `as _` conversion", |diag| {
1212
let ty_resolved = cx.typeck_results().expr_ty(expr);
13-
if let ty::Error(_) = ty_resolved.kind() {
14-
diag.help("consider giving the type explicitly");
15-
} else {
13+
if ty_resolved.is_suggestable(cx.tcx, true) {
1614
diag.span_suggestion(
1715
ty.span,
1816
"consider giving the type explicitly",
1917
ty_resolved,
2018
Applicability::MachineApplicable,
2119
);
20+
} else {
21+
diag.help("consider giving the type explicitly");
2222
}
2323
});
2424
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@no-rustfix
2+
3+
#![warn(clippy::as_underscore)]
4+
5+
fn main() {
6+
// From issue #15282
7+
let f = async || ();
8+
let _: Box<dyn FnOnce() -> _> = Box::new(f) as _;
9+
//~^ as_underscore
10+
11+
let barr = || (|| ());
12+
let _: Box<dyn Fn() -> _> = Box::new(barr) as _;
13+
//~^ as_underscore
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: using `as _` conversion
2+
--> tests/ui/as_underscore_unfixable.rs:8:37
3+
|
4+
LL | let _: Box<dyn FnOnce() -> _> = Box::new(f) as _;
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
= help: consider giving the type explicitly
8+
= note: `-D clippy::as-underscore` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::as_underscore)]`
10+
11+
error: using `as _` conversion
12+
--> tests/ui/as_underscore_unfixable.rs:12:33
13+
|
14+
LL | let _: Box<dyn Fn() -> _> = Box::new(barr) as _;
15+
| ^^^^^^^^^^^^^^^^^^^
16+
|
17+
= help: consider giving the type explicitly
18+
19+
error: aborting due to 2 previous errors
20+

0 commit comments

Comments
 (0)