Skip to content

Commit 1190c02

Browse files
committed
1 parent 371120b commit 1190c02

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

clippy_lints/src/useless_conversion.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use clippy_utils::ty::{is_copy, is_type_diagnostic_item, same_type_and_consts};
55
use clippy_utils::{get_parent_expr, is_trait_method, match_def_path, path_to_local, paths};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
8-
use rustc_hir::{BindingAnnotation, Expr, ExprKind, HirId, MatchSource, Node, PatKind};
8+
use rustc_hir::def::{DefKind, Res};
9+
use rustc_hir::{BindingAnnotation, Expr, ExprKind, HirId, MatchSource, Node, PatKind, QPath, TyKind};
910
use rustc_lint::{LateContext, LateLintPass};
1011
use rustc_middle::ty;
1112
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -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);
@@ -195,3 +197,12 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
195197
}
196198
}
197199
}
200+
201+
/// `cx.qpath_res` seems to return `AssocFn` so we do this instead
202+
fn is_ty_alias(qpath: &QPath<'_>) -> bool {
203+
match *qpath {
204+
QPath::Resolved(_, path) => matches!(path.res, Res::Def(DefKind::TyAlias, ..)),
205+
QPath::TypeRelative(ty, _) if let TyKind::Path(qpath) = ty.kind => is_ty_alias(&qpath),
206+
_ => false,
207+
}
208+
}

tests/ui/useless_conversion.fixed

+6
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

+6
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

+16-16
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)