Skip to content

Commit 1c56599

Browse files
Merge branch 'rust-lang:master' into 11710
2 parents e3ff20c + 8ee9a9c commit 1c56599

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

clippy_lints/src/methods/unnecessary_fallible_conversions.rs

+28-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::get_parent_expr;
33
use clippy_utils::ty::implements_trait;
44
use rustc_errors::Applicability;
55
use rustc_hir::{Expr, ExprKind};
66
use rustc_lint::LateContext;
77
use rustc_middle::ty;
8+
use rustc_middle::ty::print::with_forced_trimmed_paths;
89
use rustc_span::{sym, Span};
910

1011
use super::UNNECESSARY_FALLIBLE_CONVERSIONS;
@@ -42,6 +43,7 @@ fn check<'tcx>(
4243
// (else there would be conflicting impls, even with #![feature(spec)]), so we don't even need to check
4344
// what `<T as TryFrom<U>>::Error` is: it's always `Infallible`
4445
&& implements_trait(cx, self_ty, from_into_trait, &[other_ty])
46+
&& let Some(other_ty) = other_ty.as_type()
4547
{
4648
let parent_unwrap_call = get_parent_expr(cx, expr).and_then(|parent| {
4749
if let ExprKind::MethodCall(path, .., span) = parent.kind
@@ -52,8 +54,7 @@ fn check<'tcx>(
5254
None
5355
}
5456
});
55-
56-
let (sugg, span, applicability) = match kind {
57+
let (source_ty, target_ty, sugg, span, applicability) = match kind {
5758
FunctionKind::TryIntoMethod if let Some(unwrap_span) = parent_unwrap_call => {
5859
// Extend the span to include the unwrap/expect call:
5960
// `foo.try_into().expect("..")`
@@ -63,24 +64,41 @@ fn check<'tcx>(
6364
// so that can be machine-applicable
6465

6566
(
67+
self_ty,
68+
other_ty,
6669
"into()",
6770
primary_span.with_hi(unwrap_span.hi()),
6871
Applicability::MachineApplicable,
6972
)
7073
},
71-
FunctionKind::TryFromFunction => ("From::from", primary_span, Applicability::Unspecified),
72-
FunctionKind::TryIntoFunction => ("Into::into", primary_span, Applicability::Unspecified),
73-
FunctionKind::TryIntoMethod => ("into", primary_span, Applicability::Unspecified),
74+
FunctionKind::TryFromFunction => (
75+
other_ty,
76+
self_ty,
77+
"From::from",
78+
primary_span,
79+
Applicability::Unspecified,
80+
),
81+
FunctionKind::TryIntoFunction => (
82+
self_ty,
83+
other_ty,
84+
"Into::into",
85+
primary_span,
86+
Applicability::Unspecified,
87+
),
88+
FunctionKind::TryIntoMethod => (self_ty, other_ty, "into", primary_span, Applicability::Unspecified),
7489
};
7590

76-
span_lint_and_sugg(
91+
span_lint_and_then(
7792
cx,
7893
UNNECESSARY_FALLIBLE_CONVERSIONS,
7994
span,
8095
"use of a fallible conversion when an infallible one could be used",
81-
"use",
82-
sugg.into(),
83-
applicability,
96+
|diag| {
97+
with_forced_trimmed_paths!({
98+
diag.note(format!("converting `{source_ty}` to `{target_ty}` cannot fail"));
99+
});
100+
diag.span_suggestion(span, "use", sugg, applicability);
101+
},
84102
);
85103
}
86104
}

tests/ui/unnecessary_fallible_conversions.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: use of a fallible conversion when an infallible one could be used
44
LL | let _: i64 = 0i32.try_into().unwrap();
55
| ^^^^^^^^^^^^^^^^^^^ help: use: `into()`
66
|
7+
= note: converting `i32` to `i64` cannot fail
78
= note: `-D clippy::unnecessary-fallible-conversions` implied by `-D warnings`
89
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_fallible_conversions)]`
910

@@ -12,6 +13,8 @@ error: use of a fallible conversion when an infallible one could be used
1213
|
1314
LL | let _: i64 = 0i32.try_into().expect("can't happen");
1415
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `into()`
16+
|
17+
= note: converting `i32` to `i64` cannot fail
1518

1619
error: aborting due to 2 previous errors
1720

tests/ui/unnecessary_fallible_conversions_unfixable.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: use of a fallible conversion when an infallible one could be used
44
LL | let _: Result<Foo, _> = 0i64.try_into();
55
| ^^^^^^^^ help: use: `into`
66
|
7+
= note: converting `i64` to `Foo` cannot fail
78
= note: `-D clippy::unnecessary-fallible-conversions` implied by `-D warnings`
89
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_fallible_conversions)]`
910

@@ -12,30 +13,40 @@ error: use of a fallible conversion when an infallible one could be used
1213
|
1314
LL | let _: Result<Foo, _> = i64::try_into(0i64);
1415
| ^^^^^^^^^^^^^ help: use: `Into::into`
16+
|
17+
= note: converting `i64` to `Foo` cannot fail
1518

1619
error: use of a fallible conversion when an infallible one could be used
1720
--> $DIR/unnecessary_fallible_conversions_unfixable.rs:31:29
1821
|
1922
LL | let _: Result<Foo, _> = Foo::try_from(0i64);
2023
| ^^^^^^^^^^^^^ help: use: `From::from`
24+
|
25+
= note: converting `i64` to `Foo` cannot fail
2126

2227
error: use of a fallible conversion when an infallible one could be used
2328
--> $DIR/unnecessary_fallible_conversions_unfixable.rs:34:34
2429
|
2530
LL | let _: Result<i64, _> = 0i32.try_into();
2631
| ^^^^^^^^ help: use: `into`
32+
|
33+
= note: converting `i32` to `i64` cannot fail
2734

2835
error: use of a fallible conversion when an infallible one could be used
2936
--> $DIR/unnecessary_fallible_conversions_unfixable.rs:36:29
3037
|
3138
LL | let _: Result<i64, _> = i32::try_into(0i32);
3239
| ^^^^^^^^^^^^^ help: use: `Into::into`
40+
|
41+
= note: converting `i32` to `i64` cannot fail
3342

3443
error: use of a fallible conversion when an infallible one could be used
3544
--> $DIR/unnecessary_fallible_conversions_unfixable.rs:38:29
3645
|
3746
LL | let _: Result<i64, _> = <_>::try_from(0i32);
3847
| ^^^^^^^^^^^^^ help: use: `From::from`
48+
|
49+
= note: converting `i32` to `i64` cannot fail
3950

4051
error: aborting due to 6 previous errors
4152

0 commit comments

Comments
 (0)