Skip to content

Commit 526d115

Browse files
committed
Auto merge of #11191 - Alexendoo:redundant-type-annotations-ice, r=llogiq
redundant_type_annotations: only pass certain def kinds to type_of Fixes #11190 Fixes rust-lang/rust#113516 Also adds an `is_lint_allowed` check to skip the lint when it's not needed changelog: none
2 parents 6a2c8a1 + d4f735c commit 526d115

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

clippy_lints/src/redundant_type_annotations.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use clippy_utils::diagnostics::span_lint;
2+
use clippy_utils::is_lint_allowed;
23
use rustc_ast::LitKind;
34
use rustc_hir as hir;
5+
use rustc_hir::def::DefKind;
46
use rustc_lint::{LateContext, LateLintPass};
57
use rustc_middle::ty::Ty;
68
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -45,8 +47,8 @@ fn is_same_type<'tcx>(cx: &LateContext<'tcx>, ty_resolved_path: hir::def::Res, f
4547
return primty.name() == func_return_type_sym;
4648
}
4749

48-
// type annotation is any other non generic type
49-
if let hir::def::Res::Def(_, defid) = ty_resolved_path
50+
// type annotation is a non generic type
51+
if let hir::def::Res::Def(DefKind::Struct | DefKind::Union | DefKind::Enum, defid) = ty_resolved_path
5052
&& let Some(annotation_ty) = cx.tcx.type_of(defid).no_bound_vars()
5153
{
5254
return annotation_ty == func_return_type;
@@ -130,8 +132,9 @@ fn extract_primty(ty_kind: &hir::TyKind<'_>) -> Option<hir::PrimTy> {
130132

131133
impl LateLintPass<'_> for RedundantTypeAnnotations {
132134
fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx rustc_hir::Local<'tcx>) {
133-
// type annotation part
134-
if !local.span.from_expansion()
135+
if !is_lint_allowed(cx, REDUNDANT_TYPE_ANNOTATIONS, local.hir_id)
136+
// type annotation part
137+
&& !local.span.from_expansion()
135138
&& let Some(ty) = &local.ty
136139

137140
// initialization part

tests/ui/redundant_type_annotations.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ struct Cake<T> {
66
_data: T,
77
}
88

9-
fn make_something<T: Default>() -> T {
10-
T::default()
9+
fn make_something<T>() -> T {
10+
unimplemented!()
1111
}
1212

1313
fn make_cake<T: Default>() -> Cake<T> {
@@ -117,7 +117,15 @@ fn test_non_locals() {
117117
let _closure_arg = |x: u32| x;
118118
}
119119

120-
fn test_complex_types() {
120+
trait Trait {
121+
type AssocTy;
122+
}
123+
124+
impl Trait for () {
125+
type AssocTy = String;
126+
}
127+
128+
fn test_complex_types<T>() {
121129
// Shouldn't be lint, since the literal will be i32 otherwise
122130
let _u8: u8 = 128;
123131

@@ -135,6 +143,10 @@ fn test_complex_types() {
135143

136144
// Shouldn't be lint
137145
let _array: [u32; 2] = [8, 9];
146+
147+
let ty_param: T = make_something();
148+
149+
let assoc_ty: <() as Trait>::AssocTy = String::new();
138150
}
139151

140152
fn test_functions() {
@@ -173,4 +185,6 @@ fn test_simple_types() {
173185
let _var: bool = false;
174186
}
175187

188+
fn issue11190() {}
189+
176190
fn main() {}

tests/ui/redundant_type_annotations.stderr

+14-14
Original file line numberDiff line numberDiff line change
@@ -19,85 +19,85 @@ LL | let v: &Slice = self.return_a_ref_to_struct();
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020

2121
error: redundant type annotation
22-
--> $DIR/redundant_type_annotations.rs:143:5
22+
--> $DIR/redundant_type_annotations.rs:155:5
2323
|
2424
LL | let _return: String = return_a_string();
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626

2727
error: redundant type annotation
28-
--> $DIR/redundant_type_annotations.rs:145:5
28+
--> $DIR/redundant_type_annotations.rs:157:5
2929
|
3030
LL | let _return: Pie = return_a_struct();
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232

3333
error: redundant type annotation
34-
--> $DIR/redundant_type_annotations.rs:147:5
34+
--> $DIR/redundant_type_annotations.rs:159:5
3535
|
3636
LL | let _return: Pizza = return_an_enum();
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3838

3939
error: redundant type annotation
40-
--> $DIR/redundant_type_annotations.rs:149:5
40+
--> $DIR/redundant_type_annotations.rs:161:5
4141
|
4242
LL | let _return: u32 = return_an_int();
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4444

4545
error: redundant type annotation
46-
--> $DIR/redundant_type_annotations.rs:151:5
46+
--> $DIR/redundant_type_annotations.rs:163:5
4747
|
4848
LL | let _return: String = String::new();
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5050

5151
error: redundant type annotation
52-
--> $DIR/redundant_type_annotations.rs:153:5
52+
--> $DIR/redundant_type_annotations.rs:165:5
5353
|
5454
LL | let new_pie: Pie = Pie::new();
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5656

5757
error: redundant type annotation
58-
--> $DIR/redundant_type_annotations.rs:155:5
58+
--> $DIR/redundant_type_annotations.rs:167:5
5959
|
6060
LL | let _return: u32 = new_pie.return_an_int();
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6262

6363
error: redundant type annotation
64-
--> $DIR/redundant_type_annotations.rs:157:5
64+
--> $DIR/redundant_type_annotations.rs:169:5
6565
|
6666
LL | let _return: u32 = Pie::associated_return_an_int();
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6868

6969
error: redundant type annotation
70-
--> $DIR/redundant_type_annotations.rs:159:5
70+
--> $DIR/redundant_type_annotations.rs:171:5
7171
|
7272
LL | let _return: String = Pie::associated_return_a_string();
7373
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7474

7575
error: redundant type annotation
76-
--> $DIR/redundant_type_annotations.rs:165:5
76+
--> $DIR/redundant_type_annotations.rs:177:5
7777
|
7878
LL | let _var: u32 = u32::MAX;
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^
8080

8181
error: redundant type annotation
82-
--> $DIR/redundant_type_annotations.rs:167:5
82+
--> $DIR/redundant_type_annotations.rs:179:5
8383
|
8484
LL | let _var: u32 = 5_u32;
8585
| ^^^^^^^^^^^^^^^^^^^^^^
8686

8787
error: redundant type annotation
88-
--> $DIR/redundant_type_annotations.rs:169:5
88+
--> $DIR/redundant_type_annotations.rs:181:5
8989
|
9090
LL | let _var: &str = "test";
9191
| ^^^^^^^^^^^^^^^^^^^^^^^^
9292

9393
error: redundant type annotation
94-
--> $DIR/redundant_type_annotations.rs:171:5
94+
--> $DIR/redundant_type_annotations.rs:183:5
9595
|
9696
LL | let _var: &[u8] = b"test";
9797
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
9898

9999
error: redundant type annotation
100-
--> $DIR/redundant_type_annotations.rs:173:5
100+
--> $DIR/redundant_type_annotations.rs:185:5
101101
|
102102
LL | let _var: bool = false;
103103
| ^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)