Skip to content

Commit

Permalink
Add redundant type annotations lint
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessioC31 committed Mar 28, 2023
1 parent 70db226 commit a967474
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4853,6 +4853,7 @@ Released 2018-09-13
[`redundant_pub_crate`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pub_crate
[`redundant_slicing`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_slicing
[`redundant_static_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes
[`redundant_type_annotations`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_type_annotations
[`ref_binding_to_reference`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_binding_to_reference
[`ref_in_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_in_deref
[`ref_option_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_option_ref
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::redundant_slicing::DEREF_BY_SLICING_INFO,
crate::redundant_slicing::REDUNDANT_SLICING_INFO,
crate::redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES_INFO,
crate::redundant_type_annotations::REDUNDANT_TYPE_ANNOTATIONS_INFO,
crate::ref_option_ref::REF_OPTION_REF_INFO,
crate::reference::DEREF_ADDROF_INFO,
crate::regex::INVALID_REGEX_INFO,
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ mod redundant_field_names;
mod redundant_pub_crate;
mod redundant_slicing;
mod redundant_static_lifetimes;
mod redundant_type_annotations;
mod ref_option_ref;
mod reference;
mod regex;
Expand Down Expand Up @@ -940,6 +941,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|_| Box::new(allow_attributes::AllowAttribute));
store.register_late_pass(move |_| Box::new(manual_main_separator_str::ManualMainSeparatorStr::new(msrv())));
store.register_late_pass(|_| Box::new(unnecessary_struct_initialization::UnnecessaryStruct));
store.register_late_pass(|_| Box::new(redundant_type_annotations::RedundantTypeAnnotations));
// add lints here, do not remove this comment, it's used in `new_lint`
}

Expand Down
52 changes: 52 additions & 0 deletions clippy_lints/src/redundant_type_annotations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use clippy_utils::diagnostics::span_lint;

declare_clippy_lint! {
/// ### What it does
///
/// ### Why is this bad?
///
/// ### Example
/// ```rust
/// // example code where clippy issues a warning
/// ```
/// Use instead:
/// ```rust
/// // example code which does not raise clippy warning
/// ```
#[clippy::version = "1.70.0"]
pub REDUNDANT_TYPE_ANNOTATIONS,
pedantic,
"default lint description"
}
declare_lint_pass!(RedundantTypeAnnotations => [REDUNDANT_TYPE_ANNOTATIONS]);

impl LateLintPass<'_> for RedundantTypeAnnotations {
fn check_local(&mut self, cx: &LateContext<'_>, local: &'_ rustc_hir::Local<'_>) {
if_chain!(
// type annotation part
if let Some(ty) = &local.ty;
if let hir::TyKind::Path(ty_path) = &ty.kind;
if let hir::QPath::Resolved(_, resolved_path_ty) = ty_path;
if let hir::def::Res::Def(_, defid_ty) = resolved_path_ty.res;
if let Some(middle_ty) = cx.tcx.type_of(defid_ty).no_bound_vars();

// initialization part
if let Some(init) = local.init;
if let hir::ExprKind::Call(init_call, _) = init.kind;
if let hir::ExprKind::Path(init_path) = &init_call.kind;
if let hir::QPath::Resolved(_, resolved_path) = init_path;
if let hir::def::Res::Def(_, defid) = resolved_path.res;
if let Some(middle_ty_init) = cx.tcx.type_of(defid).no_bound_vars();
if middle_ty_init.is_fn();
if let Some(init_return_type) = middle_ty_init.fn_sig(cx.tcx).output().no_bound_vars();

if middle_ty == init_return_type;
then {
span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "Redundant type annotation")
}
);
}
}
28 changes: 28 additions & 0 deletions tests/ui/redundant_type_annotations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![allow(unused)]
#![warn(clippy::redundant_type_annotations)]

struct A;
enum E {
One,
Two
}

fn f() -> String {
String::new()
}

fn f_struct() -> A {
A
}

fn f_enum() -> E {
E::One
}

fn main() {
let a: String = f();
let a: A = f_struct();
let a: E = f_enum();

let b = f();
}
22 changes: 22 additions & 0 deletions tests/ui/redundant_type_annotations.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: Redundant type annotation
--> $DIR/redundant_type_annotations.rs:23:5
|
LL | let a: String = f();
| ^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::redundant-type-annotations` implied by `-D warnings`

error: Redundant type annotation
--> $DIR/redundant_type_annotations.rs:24:5
|
LL | let a: A = f_struct();
| ^^^^^^^^^^^^^^^^^^^^^^

error: Redundant type annotation
--> $DIR/redundant_type_annotations.rs:25:5
|
LL | let a: E = f_enum();
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

0 comments on commit a967474

Please sign in to comment.