-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New lint: [self_named_constructor
]
#7403
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,91 @@ | ||
use clippy_utils::diagnostics::span_lint; | ||
use clippy_utils::return_ty; | ||
use clippy_utils::ty::{contains_adt_constructor, contains_ty}; | ||
use rustc_hir::{Impl, ImplItem, ImplItemKind, ItemKind, Node}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// **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. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust,ignore | ||
/// struct Foo {} | ||
/// | ||
/// impl Foo { | ||
/// pub fn foo() -> Foo { | ||
/// Foo {} | ||
/// } | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust,ignore | ||
/// struct Foo {} | ||
/// | ||
/// impl Foo { | ||
/// pub fn new() -> Foo { | ||
/// Foo {} | ||
/// } | ||
/// } | ||
/// ``` | ||
pub SELF_NAMED_CONSTRUCTOR, | ||
style, | ||
"method should not have the same name as the type it is implemented for" | ||
} | ||
|
||
declare_lint_pass!(SelfNamedConstructor => [SELF_NAMED_CONSTRUCTOR]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructor { | ||
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) { | ||
match impl_item.kind { | ||
ImplItemKind::Fn(ref sig, _) => { | ||
if sig.decl.implicit_self.has_implicit_self() { | ||
return; | ||
} | ||
}, | ||
_ => return, | ||
} | ||
|
||
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id()); | ||
let item = cx.tcx.hir().expect_item(parent); | ||
let self_ty = cx.tcx.type_of(item.def_id); | ||
let ret_ty = return_ty(cx, impl_item.hir_id()); | ||
|
||
// Do not check trait impls | ||
if matches!(item.kind, ItemKind::Impl(Impl { of_trait: Some(_), .. })) { | ||
return; | ||
} | ||
|
||
// Ensure method is constructor-like | ||
if let Some(self_adt) = self_ty.ty_adt_def() { | ||
if !contains_adt_constructor(ret_ty, self_adt) { | ||
return; | ||
} | ||
} else if !contains_ty(ret_ty, self_ty) { | ||
return; | ||
} | ||
|
||
if_chain! { | ||
if let Some(self_def) = self_ty.ty_adt_def(); | ||
if let Some(self_local_did) = self_def.did.as_local(); | ||
let self_id = cx.tcx.hir().local_def_id_to_hir_id(self_local_did); | ||
if let Some(Node::Item(x)) = cx.tcx.hir().find(self_id); | ||
let type_name = x.ident.name.as_str().to_lowercase(); | ||
if impl_item.ident.name.as_str() == type_name || impl_item.ident.name.as_str().replace("_", "") == type_name; | ||
|
||
then { | ||
span_lint( | ||
cx, | ||
SELF_NAMED_CONSTRUCTOR, | ||
impl_item.span, | ||
&format!("constructor `{}` has the same name as the type", impl_item.ident.name), | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
struct Foo {} | ||
|
||
impl Foo { | ||
fn foo() -> Self { | ||
fn new() -> Self { | ||
impl Foo { | ||
fn bar() {} | ||
} | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,59 @@ | ||
#![warn(clippy::self_named_constructor)] | ||
|
||
struct ShouldSpawn; | ||
struct ShouldNotSpawn; | ||
|
||
impl ShouldSpawn { | ||
pub fn should_spawn() -> ShouldSpawn { | ||
ShouldSpawn | ||
} | ||
|
||
fn should_not_spawn() -> ShouldNotSpawn { | ||
ShouldNotSpawn | ||
} | ||
} | ||
|
||
impl ShouldNotSpawn { | ||
pub fn new() -> ShouldNotSpawn { | ||
ShouldNotSpawn | ||
} | ||
} | ||
|
||
struct ShouldNotSpawnWithTrait; | ||
|
||
trait ShouldNotSpawnTrait { | ||
type Item; | ||
} | ||
|
||
impl ShouldNotSpawnTrait for ShouldNotSpawnWithTrait { | ||
type Item = Self; | ||
} | ||
|
||
impl ShouldNotSpawnWithTrait { | ||
pub fn should_not_spawn_with_trait() -> impl ShouldNotSpawnTrait<Item = Self> { | ||
ShouldNotSpawnWithTrait | ||
} | ||
} | ||
|
||
// Same trait name and same type name should not spawn the lint | ||
#[derive(Default)] | ||
pub struct Default; | ||
|
||
trait TraitSameTypeName { | ||
fn should_not_spawn() -> Self; | ||
} | ||
impl TraitSameTypeName for ShouldNotSpawn { | ||
fn should_not_spawn() -> Self { | ||
ShouldNotSpawn | ||
} | ||
} | ||
|
||
struct SelfMethodShouldNotSpawn; | ||
|
||
impl SelfMethodShouldNotSpawn { | ||
fn self_method_should_not_spawn(self) -> Self { | ||
SelfMethodShouldNotSpawn | ||
} | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.