-
Notifications
You must be signed in to change notification settings - Fork 1.7k
new lint: inherent methods that should be trait impls (fixes #218) #228
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
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
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 |
---|---|---|
@@ -1,8 +1,27 @@ | ||
#![feature(plugin)] | ||
#![plugin(clippy)] | ||
|
||
#[deny(option_unwrap_used, result_unwrap_used)] | ||
#[deny(str_to_string, string_to_string)] | ||
#![allow(unused)] | ||
#![deny(clippy)] | ||
|
||
use std::ops::Mul; | ||
|
||
struct T; | ||
|
||
impl T { | ||
fn add(self, other: T) -> T { self } //~ERROR defining a method called `add` | ||
fn drop(&mut self) { } //~ERROR defining a method called `drop` | ||
|
||
fn sub(&self, other: T) -> &T { self } // no error, self is a ref | ||
fn div(self) -> T { self } // no error, different #arguments | ||
fn rem(self, other: T) { } // no error, wrong return type | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be at least one trait impl for T, e.g. |
||
impl Mul<T> for T { | ||
type Output = T; | ||
fn mul(self, other: T) -> T { self } // no error, obviously | ||
} | ||
|
||
fn main() { | ||
let opt = Some(0); | ||
let _ = opt.unwrap(); //~ERROR used unwrap() on an Option | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a
fn mul(self, other: T) -> T { self } // no error, because trait impl exists
here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. But does that make any sense? Now you have defined two
mul
methods for the type, one of which is used for*
and one for explicit.mul()
calls. Is there a use case for this?I know this can't be disallowed by the compiler, since it doesn't know about all the trait impls when compiling a crate, but I wouldn't add extra complexity to the lint just to not warn on a case that should not be written anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it should warn, but it should be a different warning, namely that it has both a
.mul(_)
method and aMul
impl and that may confuse users.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But that does not belong to this lint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, that's an acceptable position to take. In that case we should make sure that the message clearly conveys that the trait impl should be the only single implementation of this function.