Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redundant_type_annotations: only pass certain def kinds to type_of #11191

Merged
merged 1 commit into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions clippy_lints/src/redundant_type_annotations.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::is_lint_allowed;
use rustc_ast::LitKind;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::Ty;
use rustc_session::{declare_lint_pass, declare_tool_lint};
Expand Down Expand Up @@ -45,8 +47,8 @@ fn is_same_type<'tcx>(cx: &LateContext<'tcx>, ty_resolved_path: hir::def::Res, f
return primty.name() == func_return_type_sym;
}

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

impl LateLintPass<'_> for RedundantTypeAnnotations {
fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx rustc_hir::Local<'tcx>) {
// type annotation part
if !local.span.from_expansion()
if !is_lint_allowed(cx, REDUNDANT_TYPE_ANNOTATIONS, local.hir_id)
// type annotation part
&& !local.span.from_expansion()
&& let Some(ty) = &local.ty

// initialization part
Expand Down
20 changes: 17 additions & 3 deletions tests/ui/redundant_type_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ struct Cake<T> {
_data: T,
}

fn make_something<T: Default>() -> T {
T::default()
fn make_something<T>() -> T {
unimplemented!()
}

fn make_cake<T: Default>() -> Cake<T> {
Expand Down Expand Up @@ -117,7 +117,15 @@ fn test_non_locals() {
let _closure_arg = |x: u32| x;
}

fn test_complex_types() {
trait Trait {
type AssocTy;
}

impl Trait for () {
type AssocTy = String;
}

fn test_complex_types<T>() {
// Shouldn't be lint, since the literal will be i32 otherwise
let _u8: u8 = 128;

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

// Shouldn't be lint
let _array: [u32; 2] = [8, 9];

let ty_param: T = make_something();

let assoc_ty: <() as Trait>::AssocTy = String::new();
}

fn test_functions() {
Expand Down Expand Up @@ -173,4 +185,6 @@ fn test_simple_types() {
let _var: bool = false;
}

fn issue11190() {}

fn main() {}
28 changes: 14 additions & 14 deletions tests/ui/redundant_type_annotations.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,85 +19,85 @@ LL | let v: &Slice = self.return_a_ref_to_struct();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:143:5
--> $DIR/redundant_type_annotations.rs:155:5
|
LL | let _return: String = return_a_string();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:145:5
--> $DIR/redundant_type_annotations.rs:157:5
|
LL | let _return: Pie = return_a_struct();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:147:5
--> $DIR/redundant_type_annotations.rs:159:5
|
LL | let _return: Pizza = return_an_enum();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:149:5
--> $DIR/redundant_type_annotations.rs:161:5
|
LL | let _return: u32 = return_an_int();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:151:5
--> $DIR/redundant_type_annotations.rs:163:5
|
LL | let _return: String = String::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:153:5
--> $DIR/redundant_type_annotations.rs:165:5
|
LL | let new_pie: Pie = Pie::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

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

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

error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:165:5
--> $DIR/redundant_type_annotations.rs:177:5
|
LL | let _var: u32 = u32::MAX;
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:167:5
--> $DIR/redundant_type_annotations.rs:179:5
|
LL | let _var: u32 = 5_u32;
| ^^^^^^^^^^^^^^^^^^^^^^

error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:169:5
--> $DIR/redundant_type_annotations.rs:181:5
|
LL | let _var: &str = "test";
| ^^^^^^^^^^^^^^^^^^^^^^^^

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

error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:173:5
--> $DIR/redundant_type_annotations.rs:185:5
|
LL | let _var: bool = false;
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand Down