Auto merge of #11767 - matthri:unnecessary-fallible-conversions-ext-notes, r=blyxyas

Add type details to unnecessary_fallible_conversions note

fixes: #11753

changelog: [`unnecessary_fallible_conversions`]: add type details to lint note
This commit is contained in:
bors 2023-11-11 22:51:27 +00:00
commit 8ee9a9c549
3 changed files with 42 additions and 10 deletions

View File

@ -1,10 +1,11 @@
use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::get_parent_expr; use clippy_utils::get_parent_expr;
use clippy_utils::ty::implements_trait; use clippy_utils::ty::implements_trait;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind}; use rustc_hir::{Expr, ExprKind};
use rustc_lint::LateContext; use rustc_lint::LateContext;
use rustc_middle::ty; use rustc_middle::ty;
use rustc_middle::ty::print::with_forced_trimmed_paths;
use rustc_span::{sym, Span}; use rustc_span::{sym, Span};
use super::UNNECESSARY_FALLIBLE_CONVERSIONS; use super::UNNECESSARY_FALLIBLE_CONVERSIONS;
@ -42,6 +43,7 @@ fn check<'tcx>(
// (else there would be conflicting impls, even with #![feature(spec)]), so we don't even need to check // (else there would be conflicting impls, even with #![feature(spec)]), so we don't even need to check
// what `<T as TryFrom<U>>::Error` is: it's always `Infallible` // what `<T as TryFrom<U>>::Error` is: it's always `Infallible`
&& implements_trait(cx, self_ty, from_into_trait, &[other_ty]) && implements_trait(cx, self_ty, from_into_trait, &[other_ty])
&& let Some(other_ty) = other_ty.as_type()
{ {
let parent_unwrap_call = get_parent_expr(cx, expr).and_then(|parent| { let parent_unwrap_call = get_parent_expr(cx, expr).and_then(|parent| {
if let ExprKind::MethodCall(path, .., span) = parent.kind if let ExprKind::MethodCall(path, .., span) = parent.kind
@ -52,8 +54,7 @@ fn check<'tcx>(
None None
} }
}); });
let (source_ty, target_ty, sugg, span, applicability) = match kind {
let (sugg, span, applicability) = match kind {
FunctionKind::TryIntoMethod if let Some(unwrap_span) = parent_unwrap_call => { FunctionKind::TryIntoMethod if let Some(unwrap_span) = parent_unwrap_call => {
// Extend the span to include the unwrap/expect call: // Extend the span to include the unwrap/expect call:
// `foo.try_into().expect("..")` // `foo.try_into().expect("..")`
@ -63,24 +64,41 @@ fn check<'tcx>(
// so that can be machine-applicable // so that can be machine-applicable
( (
self_ty,
other_ty,
"into()", "into()",
primary_span.with_hi(unwrap_span.hi()), primary_span.with_hi(unwrap_span.hi()),
Applicability::MachineApplicable, Applicability::MachineApplicable,
) )
}, },
FunctionKind::TryFromFunction => ("From::from", primary_span, Applicability::Unspecified), FunctionKind::TryFromFunction => (
FunctionKind::TryIntoFunction => ("Into::into", primary_span, Applicability::Unspecified), other_ty,
FunctionKind::TryIntoMethod => ("into", primary_span, Applicability::Unspecified), self_ty,
"From::from",
primary_span,
Applicability::Unspecified,
),
FunctionKind::TryIntoFunction => (
self_ty,
other_ty,
"Into::into",
primary_span,
Applicability::Unspecified,
),
FunctionKind::TryIntoMethod => (self_ty, other_ty, "into", primary_span, Applicability::Unspecified),
}; };
span_lint_and_sugg( span_lint_and_then(
cx, cx,
UNNECESSARY_FALLIBLE_CONVERSIONS, UNNECESSARY_FALLIBLE_CONVERSIONS,
span, span,
"use of a fallible conversion when an infallible one could be used", "use of a fallible conversion when an infallible one could be used",
"use", |diag| {
sugg.into(), with_forced_trimmed_paths!({
applicability, diag.note(format!("converting `{source_ty}` to `{target_ty}` cannot fail"));
});
diag.span_suggestion(span, "use", sugg, applicability);
},
); );
} }
} }

View File

@ -4,6 +4,7 @@ error: use of a fallible conversion when an infallible one could be used
LL | let _: i64 = 0i32.try_into().unwrap(); LL | let _: i64 = 0i32.try_into().unwrap();
| ^^^^^^^^^^^^^^^^^^^ help: use: `into()` | ^^^^^^^^^^^^^^^^^^^ help: use: `into()`
| |
= note: converting `i32` to `i64` cannot fail
= note: `-D clippy::unnecessary-fallible-conversions` implied by `-D warnings` = note: `-D clippy::unnecessary-fallible-conversions` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_fallible_conversions)]` = help: to override `-D warnings` add `#[allow(clippy::unnecessary_fallible_conversions)]`
@ -12,6 +13,8 @@ error: use of a fallible conversion when an infallible one could be used
| |
LL | let _: i64 = 0i32.try_into().expect("can't happen"); LL | let _: i64 = 0i32.try_into().expect("can't happen");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `into()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `into()`
|
= note: converting `i32` to `i64` cannot fail
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View File

@ -4,6 +4,7 @@ error: use of a fallible conversion when an infallible one could be used
LL | let _: Result<Foo, _> = 0i64.try_into(); LL | let _: Result<Foo, _> = 0i64.try_into();
| ^^^^^^^^ help: use: `into` | ^^^^^^^^ help: use: `into`
| |
= note: converting `i64` to `Foo` cannot fail
= note: `-D clippy::unnecessary-fallible-conversions` implied by `-D warnings` = note: `-D clippy::unnecessary-fallible-conversions` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_fallible_conversions)]` = help: to override `-D warnings` add `#[allow(clippy::unnecessary_fallible_conversions)]`
@ -12,30 +13,40 @@ error: use of a fallible conversion when an infallible one could be used
| |
LL | let _: Result<Foo, _> = i64::try_into(0i64); LL | let _: Result<Foo, _> = i64::try_into(0i64);
| ^^^^^^^^^^^^^ help: use: `Into::into` | ^^^^^^^^^^^^^ help: use: `Into::into`
|
= note: converting `i64` to `Foo` cannot fail
error: use of a fallible conversion when an infallible one could be used error: use of a fallible conversion when an infallible one could be used
--> $DIR/unnecessary_fallible_conversions_unfixable.rs:31:29 --> $DIR/unnecessary_fallible_conversions_unfixable.rs:31:29
| |
LL | let _: Result<Foo, _> = Foo::try_from(0i64); LL | let _: Result<Foo, _> = Foo::try_from(0i64);
| ^^^^^^^^^^^^^ help: use: `From::from` | ^^^^^^^^^^^^^ help: use: `From::from`
|
= note: converting `i64` to `Foo` cannot fail
error: use of a fallible conversion when an infallible one could be used error: use of a fallible conversion when an infallible one could be used
--> $DIR/unnecessary_fallible_conversions_unfixable.rs:34:34 --> $DIR/unnecessary_fallible_conversions_unfixable.rs:34:34
| |
LL | let _: Result<i64, _> = 0i32.try_into(); LL | let _: Result<i64, _> = 0i32.try_into();
| ^^^^^^^^ help: use: `into` | ^^^^^^^^ help: use: `into`
|
= note: converting `i32` to `i64` cannot fail
error: use of a fallible conversion when an infallible one could be used error: use of a fallible conversion when an infallible one could be used
--> $DIR/unnecessary_fallible_conversions_unfixable.rs:36:29 --> $DIR/unnecessary_fallible_conversions_unfixable.rs:36:29
| |
LL | let _: Result<i64, _> = i32::try_into(0i32); LL | let _: Result<i64, _> = i32::try_into(0i32);
| ^^^^^^^^^^^^^ help: use: `Into::into` | ^^^^^^^^^^^^^ help: use: `Into::into`
|
= note: converting `i32` to `i64` cannot fail
error: use of a fallible conversion when an infallible one could be used error: use of a fallible conversion when an infallible one could be used
--> $DIR/unnecessary_fallible_conversions_unfixable.rs:38:29 --> $DIR/unnecessary_fallible_conversions_unfixable.rs:38:29
| |
LL | let _: Result<i64, _> = <_>::try_from(0i32); LL | let _: Result<i64, _> = <_>::try_from(0i32);
| ^^^^^^^^^^^^^ help: use: `From::from` | ^^^^^^^^^^^^^ help: use: `From::from`
|
= note: converting `i32` to `i64` cannot fail
error: aborting due to 6 previous errors error: aborting due to 6 previous errors