Skip to content

Commit 590c9fc

Browse files
committed
Auto merge of rust-lang#10778 - Centri3:type-alias-fix, r=llogiq
Don't emit clippy::useless_conversion on type aliases Fixes rust-lang#10773 changelog: Enhancement: [`useless_conversion`]: Don't lint on type aliases
2 parents a167973 + a36d9a7 commit 590c9fc

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed

clippy_lints/src/useless_conversion.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
2+
use clippy_utils::is_ty_alias;
23
use clippy_utils::source::{snippet, snippet_with_context};
34
use clippy_utils::sugg::Sugg;
45
use clippy_utils::ty::{is_copy, is_type_diagnostic_item, same_type_and_consts};
@@ -138,6 +139,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
138139
if_chain! {
139140
if let ExprKind::Path(ref qpath) = path.kind;
140141
if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id();
142+
if !is_ty_alias(qpath);
141143
then {
142144
let a = cx.typeck_results().expr_ty(e);
143145
let b = cx.typeck_results().expr_ty(arg);

clippy_utils/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(array_chunks)]
22
#![feature(box_patterns)]
3+
#![feature(if_let_guard)]
34
#![feature(let_chains)]
45
#![feature(lint_reasons)]
56
#![feature(never_type)]
@@ -282,6 +283,15 @@ pub fn is_wild(pat: &Pat<'_>) -> bool {
282283
matches!(pat.kind, PatKind::Wild)
283284
}
284285

286+
/// Checks if the given `QPath` belongs to a type alias.
287+
pub fn is_ty_alias(qpath: &QPath<'_>) -> bool {
288+
match *qpath {
289+
QPath::Resolved(_, path) => matches!(path.res, Res::Def(DefKind::TyAlias, ..)),
290+
QPath::TypeRelative(ty, _) if let TyKind::Path(qpath) = ty.kind => { is_ty_alias(&qpath) },
291+
_ => false,
292+
}
293+
}
294+
285295
/// Checks if the method call given in `expr` belongs to the given trait.
286296
/// This is a deprecated function, consider using [`is_trait_method`].
287297
pub fn match_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, path: &[&str]) -> bool {

tests/ui/useless_conversion.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ fn test_issue_3913() -> Result<(), std::io::Error> {
3333
Ok(())
3434
}
3535

36+
fn dont_lint_on_type_alias() {
37+
type A = i32;
38+
_ = A::from(0i32);
39+
}
40+
3641
fn dont_lint_into_iter_on_immutable_local_implementing_iterator_in_expr() {
3742
let text = "foo\r\nbar\n\nbaz\n";
3843
let lines = text.lines();
@@ -106,6 +111,7 @@ fn main() {
106111
test_questionmark().unwrap();
107112
test_issue_3913().unwrap();
108113

114+
dont_lint_on_type_alias();
109115
dont_lint_into_iter_on_immutable_local_implementing_iterator_in_expr();
110116
lint_into_iter_on_mutable_local_implementing_iterator_in_expr();
111117
lint_into_iter_on_expr_implementing_iterator();

tests/ui/useless_conversion.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ fn test_issue_3913() -> Result<(), std::io::Error> {
3333
Ok(())
3434
}
3535

36+
fn dont_lint_on_type_alias() {
37+
type A = i32;
38+
_ = A::from(0i32);
39+
}
40+
3641
fn dont_lint_into_iter_on_immutable_local_implementing_iterator_in_expr() {
3742
let text = "foo\r\nbar\n\nbaz\n";
3843
let lines = text.lines();
@@ -106,6 +111,7 @@ fn main() {
106111
test_questionmark().unwrap();
107112
test_issue_3913().unwrap();
108113

114+
dont_lint_on_type_alias();
109115
dont_lint_into_iter_on_immutable_local_implementing_iterator_in_expr();
110116
lint_into_iter_on_mutable_local_implementing_iterator_in_expr();
111117
lint_into_iter_on_expr_implementing_iterator();

tests/ui/useless_conversion.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,97 +23,97 @@ LL | let _: i32 = 0i32.into();
2323
| ^^^^^^^^^^^ help: consider removing `.into()`: `0i32`
2424

2525
error: useless conversion to the same type: `std::str::Lines<'_>`
26-
--> $DIR/useless_conversion.rs:45:22
26+
--> $DIR/useless_conversion.rs:50:22
2727
|
2828
LL | if Some("ok") == lines.into_iter().next() {}
2929
| ^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `lines`
3030

3131
error: useless conversion to the same type: `std::str::Lines<'_>`
32-
--> $DIR/useless_conversion.rs:50:21
32+
--> $DIR/useless_conversion.rs:55:21
3333
|
3434
LL | let mut lines = text.lines().into_iter();
3535
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `text.lines()`
3636

3737
error: useless conversion to the same type: `std::str::Lines<'_>`
38-
--> $DIR/useless_conversion.rs:56:22
38+
--> $DIR/useless_conversion.rs:61:22
3939
|
4040
LL | if Some("ok") == text.lines().into_iter().next() {}
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `text.lines()`
4242

4343
error: useless conversion to the same type: `std::ops::Range<i32>`
44-
--> $DIR/useless_conversion.rs:62:13
44+
--> $DIR/useless_conversion.rs:67:13
4545
|
4646
LL | let _ = NUMBERS.into_iter().next();
4747
| ^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `NUMBERS`
4848

4949
error: useless conversion to the same type: `std::ops::Range<i32>`
50-
--> $DIR/useless_conversion.rs:67:17
50+
--> $DIR/useless_conversion.rs:72:17
5151
|
5252
LL | let mut n = NUMBERS.into_iter();
5353
| ^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `NUMBERS`
5454

5555
error: useless conversion to the same type: `std::string::String`
56-
--> $DIR/useless_conversion.rs:128:21
56+
--> $DIR/useless_conversion.rs:134:21
5757
|
5858
LL | let _: String = "foo".to_string().into();
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `"foo".to_string()`
6060

6161
error: useless conversion to the same type: `std::string::String`
62-
--> $DIR/useless_conversion.rs:129:21
62+
--> $DIR/useless_conversion.rs:135:21
6363
|
6464
LL | let _: String = From::from("foo".to_string());
6565
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `From::from()`: `"foo".to_string()`
6666

6767
error: useless conversion to the same type: `std::string::String`
68-
--> $DIR/useless_conversion.rs:130:13
68+
--> $DIR/useless_conversion.rs:136:13
6969
|
7070
LL | let _ = String::from("foo".to_string());
7171
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `"foo".to_string()`
7272

7373
error: useless conversion to the same type: `std::string::String`
74-
--> $DIR/useless_conversion.rs:131:13
74+
--> $DIR/useless_conversion.rs:137:13
7575
|
7676
LL | let _ = String::from(format!("A: {:04}", 123));
7777
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `format!("A: {:04}", 123)`
7878

7979
error: useless conversion to the same type: `std::str::Lines<'_>`
80-
--> $DIR/useless_conversion.rs:132:13
80+
--> $DIR/useless_conversion.rs:138:13
8181
|
8282
LL | let _ = "".lines().into_iter();
8383
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `"".lines()`
8484

8585
error: useless conversion to the same type: `std::vec::IntoIter<i32>`
86-
--> $DIR/useless_conversion.rs:133:13
86+
--> $DIR/useless_conversion.rs:139:13
8787
|
8888
LL | let _ = vec![1, 2, 3].into_iter().into_iter();
8989
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2, 3].into_iter()`
9090

9191
error: useless conversion to the same type: `std::string::String`
92-
--> $DIR/useless_conversion.rs:134:21
92+
--> $DIR/useless_conversion.rs:140:21
9393
|
9494
LL | let _: String = format!("Hello {}", "world").into();
9595
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `format!("Hello {}", "world")`
9696

9797
error: useless conversion to the same type: `i32`
98-
--> $DIR/useless_conversion.rs:139:13
98+
--> $DIR/useless_conversion.rs:145:13
9999
|
100100
LL | let _ = i32::from(a + b) * 3;
101101
| ^^^^^^^^^^^^^^^^ help: consider removing `i32::from()`: `(a + b)`
102102

103103
error: useless conversion to the same type: `Foo<'a'>`
104-
--> $DIR/useless_conversion.rs:145:23
104+
--> $DIR/useless_conversion.rs:151:23
105105
|
106106
LL | let _: Foo<'a'> = s2.into();
107107
| ^^^^^^^^^ help: consider removing `.into()`: `s2`
108108

109109
error: useless conversion to the same type: `Foo<'a'>`
110-
--> $DIR/useless_conversion.rs:147:13
110+
--> $DIR/useless_conversion.rs:153:13
111111
|
112112
LL | let _ = Foo::<'a'>::from(s3);
113113
| ^^^^^^^^^^^^^^^^^^^^ help: consider removing `Foo::<'a'>::from()`: `s3`
114114

115115
error: useless conversion to the same type: `std::vec::IntoIter<Foo<'a'>>`
116-
--> $DIR/useless_conversion.rs:149:13
116+
--> $DIR/useless_conversion.rs:155:13
117117
|
118118
LL | let _ = vec![s4, s4, s4].into_iter().into_iter();
119119
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![s4, s4, s4].into_iter()`

0 commit comments

Comments
 (0)