Skip to content

Commit

Permalink
Rename and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Huang committed Jun 28, 2021
1 parent 96b6f2e commit c9ae73e
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2750,7 +2750,6 @@ Released 2018-09-13
[`redundant_closure_for_method_calls`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls
[`redundant_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_else
[`redundant_field_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names
[`redundant_method_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_method_names
[`redundant_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern
[`redundant_pattern_matching`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching
[`redundant_pub_crate`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pub_crate
Expand All @@ -2771,6 +2770,7 @@ Released 2018-09-13
[`same_item_push`]: https://rust-lang.github.io/rust-clippy/master/index.html#same_item_push
[`search_is_some`]: https://rust-lang.github.io/rust-clippy/master/index.html#search_is_some
[`self_assignment`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_assignment
[`self_named_constructor`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_named_constructor
[`semicolon_if_nothing_returned`]: https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
[`serde_api_misuse`]: https://rust-lang.github.io/rust-clippy/master/index.html#serde_api_misuse
[`shadow_reuse`]: https://rust-lang.github.io/rust-clippy/master/index.html#shadow_reuse
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ declare_clippy_lint! {
"binding initialized with Default should have its fields set in the initializer"
}

#[allow(clippy::redundant_method_names)]
#[allow(clippy::self_named_constructor)]
#[derive(Default)]
pub struct Default {
// Spans linted by `field_reassign_with_default`.
Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ mod redundant_clone;
mod redundant_closure_call;
mod redundant_else;
mod redundant_field_names;
mod redundant_method_names;
mod redundant_pub_crate;
mod redundant_slicing;
mod redundant_static_lifetimes;
Expand All @@ -325,6 +324,7 @@ mod regex;
mod repeat_once;
mod returns;
mod self_assignment;
mod self_named_constructor;
mod semicolon_if_nothing_returned;
mod serde_api;
mod shadow;
Expand Down Expand Up @@ -881,7 +881,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
redundant_closure_call::REDUNDANT_CLOSURE_CALL,
redundant_else::REDUNDANT_ELSE,
redundant_field_names::REDUNDANT_FIELD_NAMES,
redundant_method_names::REDUNDANT_METHOD_NAMES,
redundant_pub_crate::REDUNDANT_PUB_CRATE,
redundant_slicing::REDUNDANT_SLICING,
redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES,
Expand All @@ -894,6 +893,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
returns::LET_AND_RETURN,
returns::NEEDLESS_RETURN,
self_assignment::SELF_ASSIGNMENT,
self_named_constructor::SELF_NAMED_CONSTRUCTOR,
semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED,
serde_api::SERDE_API_MISUSE,
shadow::SHADOW_REUSE,
Expand Down Expand Up @@ -1388,7 +1388,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(redundant_clone::REDUNDANT_CLONE),
LintId::of(redundant_closure_call::REDUNDANT_CLOSURE_CALL),
LintId::of(redundant_field_names::REDUNDANT_FIELD_NAMES),
LintId::of(redundant_method_names::REDUNDANT_METHOD_NAMES),
LintId::of(redundant_slicing::REDUNDANT_SLICING),
LintId::of(redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES),
LintId::of(reference::DEREF_ADDROF),
Expand All @@ -1398,6 +1397,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(returns::LET_AND_RETURN),
LintId::of(returns::NEEDLESS_RETURN),
LintId::of(self_assignment::SELF_ASSIGNMENT),
LintId::of(self_named_constructor::SELF_NAMED_CONSTRUCTOR),
LintId::of(serde_api::SERDE_API_MISUSE),
LintId::of(single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS),
LintId::of(size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT),
Expand Down Expand Up @@ -1553,10 +1553,10 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(question_mark::QUESTION_MARK),
LintId::of(ranges::MANUAL_RANGE_CONTAINS),
LintId::of(redundant_field_names::REDUNDANT_FIELD_NAMES),
LintId::of(redundant_method_names::REDUNDANT_METHOD_NAMES),
LintId::of(redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES),
LintId::of(returns::LET_AND_RETURN),
LintId::of(returns::NEEDLESS_RETURN),
LintId::of(self_named_constructor::SELF_NAMED_CONSTRUCTOR),
LintId::of(single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS),
LintId::of(tabs_in_doc_comments::TABS_IN_DOC_COMMENTS),
LintId::of(to_digit_is_some::TO_DIGIT_IS_SOME),
Expand Down Expand Up @@ -2086,7 +2086,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(move || box disallowed_type::DisallowedType::new(&disallowed_types));
let import_renames = conf.enforced_import_renames.clone();
store.register_late_pass(move || box missing_enforced_import_rename::ImportRename::new(import_renames.clone()));
store.register_late_pass(move || box redundant_method_names::RedundantMethodNames);
store.register_late_pass(move || box self_named_constructor::SelfNamedConstructor);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::Symbol;

declare_clippy_lint! {
/// **What it does:** Warns when methods have the same name as their types.
/// **What it does:** Warns when constructors have the same name as their types.
///
/// **Why is this bad?** Repeating the name of the type is redundant.
///
Expand All @@ -33,14 +33,14 @@ declare_clippy_lint! {
/// }
/// }
/// ```
pub REDUNDANT_METHOD_NAMES,
pub SELF_NAMED_CONSTRUCTOR,
style,
"method should not have the same name as the type it is implemented for"
}

declare_lint_pass!(RedundantMethodNames => [REDUNDANT_METHOD_NAMES]);
declare_lint_pass!(SelfNamedConstructor => [SELF_NAMED_CONSTRUCTOR]);

impl<'tcx> LateLintPass<'tcx> for RedundantMethodNames {
impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructor {
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {
if_chain! {
// Get type name
Expand All @@ -51,15 +51,15 @@ impl<'tcx> LateLintPass<'tcx> for RedundantMethodNames {
let ty_hir_id = cx.tcx.hir().local_def_id_to_hir_id(local_id);
if let Some(Node::Item(x)) = cx.tcx.hir().find(ty_hir_id);

// Get method name
// Get constructor name
if let ImplItemKind::Fn(_, _) = impl_item.kind;
if impl_item.ident.name == Symbol::intern(&x.ident.name.as_str().to_lowercase());
then {
span_lint_and_then(
cx,
REDUNDANT_METHOD_NAMES,
SELF_NAMED_CONSTRUCTOR,
impl_item.span,
&format!("method `{}` has the same name as the type it is implemented for", impl_item.ident.name),
&format!("constructor `{}` has the same name as the type", impl_item.ident.name),
|diag| {
diag.span_help(impl_item.span, &format!("consider renaming `{}()` to `new()`", impl_item.ident.name));
},
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/needless_bool/fixable.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
clippy::no_effect,
clippy::if_same_then_else,
clippy::needless_return,
clippy::redundant_method_names
clippy::self_named_constructor
)]

use std::cell::Cell;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/needless_bool/fixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
clippy::no_effect,
clippy::if_same_then_else,
clippy::needless_return,
clippy::redundant_method_names
clippy::self_named_constructor
)]

use std::cell::Cell;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![warn(clippy::redundant_method_names)]
#![warn(clippy::self_named_constructor)]

struct Foo {}

Expand Down Expand Up @@ -27,4 +27,10 @@ impl FooBar {
#[derive(Default)]
pub struct Default {}

impl Foo {
fn bar() -> Bar {
Bar {}
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
error: method `foo` has the same name as the type it is implemented for
--> $DIR/redundant_method_names.rs:6:5
error: constructor `foo` has the same name as the type
--> $DIR/self_named_constructor.rs:6:5
|
LL | / pub fn foo() -> Foo {
LL | | Foo {}
LL | | }
| |_____^
|
= note: `-D clippy::redundant-method-names` implied by `-D warnings`
= note: `-D clippy::self-named-constructor` implied by `-D warnings`
help: consider renaming `foo()` to `new()`
--> $DIR/redundant_method_names.rs:6:5
--> $DIR/self_named_constructor.rs:6:5
|
LL | / pub fn foo() -> Foo {
LL | | Foo {}
LL | | }
| |_____^

error: method `foobar` has the same name as the type it is implemented for
--> $DIR/redundant_method_names.rs:22:5
error: constructor `foobar` has the same name as the type
--> $DIR/self_named_constructor.rs:22:5
|
LL | / pub fn foobar() -> FooBar {
LL | | FooBar {}
LL | | }
| |_____^
|
help: consider renaming `foobar()` to `new()`
--> $DIR/redundant_method_names.rs:22:5
--> $DIR/self_named_constructor.rs:22:5
|
LL | / pub fn foobar() -> FooBar {
LL | | FooBar {}
LL | | }
| |_____^

error: method `default` has the same name as the type it is implemented for
--> $DIR/redundant_method_names.rs:27:10
error: constructor `default` has the same name as the type
--> $DIR/self_named_constructor.rs:27:10
|
LL | #[derive(Default)]
| ^^^^^^^
|
help: consider renaming `default()` to `new()`
--> $DIR/redundant_method_names.rs:27:10
--> $DIR/self_named_constructor.rs:27:10
|
LL | #[derive(Default)]
| ^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/unit_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
clippy::unnecessary_wraps,
clippy::or_fun_call,
clippy::needless_question_mark,
clippy::redundant_method_names
clippy::self_named_constructor
)]

use std::fmt::Debug;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/use_self.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
clippy::should_implement_trait,
clippy::upper_case_acronyms,
clippy::from_over_into,
clippy::redundant_method_names
clippy::self_named_constructor
)]

#[macro_use]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
clippy::should_implement_trait,
clippy::upper_case_acronyms,
clippy::from_over_into,
clippy::redundant_method_names
clippy::self_named_constructor
)]

#[macro_use]
Expand Down

0 comments on commit c9ae73e

Please sign in to comment.