forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70db226
commit a967474
Showing
6 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|