Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions clippy_lints/src/casts/as_underscore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ use clippy_utils::diagnostics::span_lint_and_then;
use rustc_errors::Applicability;
use rustc_hir::{Expr, Ty, TyKind};
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_middle::ty::IsSuggestable;

use super::AS_UNDERSCORE;

pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, ty: &'tcx Ty<'_>) {
if matches!(ty.kind, TyKind::Infer(())) {
span_lint_and_then(cx, AS_UNDERSCORE, expr.span, "using `as _` conversion", |diag| {
let ty_resolved = cx.typeck_results().expr_ty(expr);
if let ty::Error(_) = ty_resolved.kind() {
diag.help("consider giving the type explicitly");
} else {
if ty_resolved.is_suggestable(cx.tcx, true) {
diag.span_suggestion(
ty.span,
"consider giving the type explicitly",
ty_resolved,
Applicability::MachineApplicable,
);
} else {
diag.help("consider giving the type explicitly");
}
});
}
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/as_underscore_unfixable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@no-rustfix

#![warn(clippy::as_underscore)]

fn main() {
// From issue #15282
let f = async || ();
let _: Box<dyn FnOnce() -> _> = Box::new(f) as _;
//~^ as_underscore

let barr = || (|| ());
let _: Box<dyn Fn() -> _> = Box::new(barr) as _;
//~^ as_underscore
}
20 changes: 20 additions & 0 deletions tests/ui/as_underscore_unfixable.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: using `as _` conversion
--> tests/ui/as_underscore_unfixable.rs:8:37
|
LL | let _: Box<dyn FnOnce() -> _> = Box::new(f) as _;
| ^^^^^^^^^^^^^^^^
|
= help: consider giving the type explicitly
= note: `-D clippy::as-underscore` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::as_underscore)]`

error: using `as _` conversion
--> tests/ui/as_underscore_unfixable.rs:12:33
|
LL | let _: Box<dyn Fn() -> _> = Box::new(barr) as _;
| ^^^^^^^^^^^^^^^^^^^
|
= help: consider giving the type explicitly

error: aborting due to 2 previous errors