-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Added from_over_into lint #6476
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 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 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,83 @@ | ||
use crate::utils::paths::INTO; | ||
use crate::utils::{match_def_path, meets_msrv, span_lint_and_help}; | ||
use if_chain::if_chain; | ||
use rustc_hir as hir; | ||
use rustc_lint::{LateContext, LateLintPass, LintContext}; | ||
use rustc_semver::RustcVersion; | ||
use rustc_session::{declare_tool_lint, impl_lint_pass}; | ||
|
||
const FROM_OVER_INTO_MSRV: RustcVersion = RustcVersion::new(1, 41, 0); | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Searches for implementations of the `Into<..>` trait and suggests to implement `From<..>` instead. | ||
/// | ||
/// **Why is this bad?** According the std docs implementing `From<..>` is preferred since it gives you `Into<..>` for free where the reverse isn't true. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// struct StringWrapper(String); | ||
/// | ||
/// impl Into<StringWrapper> for String { | ||
/// fn into(self) -> StringWrapper { | ||
/// StringWrapper(self) | ||
/// } | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// struct StringWrapper(String); | ||
/// | ||
/// impl From<String> for StringWrapper { | ||
/// fn from(s: String) -> StringWrapper { | ||
/// StringWrapper(s) | ||
/// } | ||
/// } | ||
/// ``` | ||
pub FROM_OVER_INTO, | ||
style, | ||
"Warns on implementations of `Into<..>` to use `From<..>`" | ||
} | ||
|
||
pub struct FromOverInto { | ||
msrv: Option<RustcVersion>, | ||
} | ||
|
||
impl FromOverInto { | ||
#[must_use] | ||
pub fn new(msrv: Option<RustcVersion>) -> Self { | ||
FromOverInto { msrv } | ||
} | ||
} | ||
|
||
impl_lint_pass!(FromOverInto => [FROM_OVER_INTO]); | ||
|
||
impl LateLintPass<'_> for FromOverInto { | ||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) { | ||
if !meets_msrv(self.msrv.as_ref(), &FROM_OVER_INTO_MSRV) { | ||
return; | ||
} | ||
|
||
let impl_def_id = cx.tcx.hir().local_def_id(item.hir_id); | ||
if_chain! { | ||
if let hir::ItemKind::Impl{ .. } = &item.kind; | ||
if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(impl_def_id); | ||
if match_def_path(cx, impl_trait_ref.def_id, &INTO); | ||
|
||
then { | ||
span_lint_and_help( | ||
cx, | ||
FROM_OVER_INTO, | ||
item.span, | ||
"An implementation of From is preferred since it gives you Into<..> for free where the reverse isn't true.", | ||
1c3t3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
None, | ||
"consider to implement From instead", | ||
1c3t3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
} | ||
} | ||
} | ||
|
||
extract_msrv_attr!(LateContext); | ||
} |
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,21 @@ | ||
#![warn(clippy::from_over_into)] | ||
|
||
// this should throw an error | ||
struct StringWrapper(String); | ||
|
||
impl Into<StringWrapper> for String { | ||
fn into(self) -> StringWrapper { | ||
StringWrapper(self) | ||
} | ||
} | ||
|
||
// this is fine | ||
struct A(String); | ||
|
||
impl From<String> for A { | ||
fn from(s: String) -> A { | ||
A(s) | ||
} | ||
} | ||
|
||
fn main() {} |
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,15 @@ | ||
error: An implementation of From is preferred since it gives you Into<..> for free where the reverse isn't true. | ||
--> $DIR/from_over_into.rs:6:1 | ||
| | ||
LL | / impl Into<StringWrapper> for String { | ||
LL | | fn into(self) -> StringWrapper { | ||
LL | | StringWrapper(self) | ||
LL | | } | ||
LL | | } | ||
| |_^ | ||
| | ||
= note: `-D clippy::from-over-into` implied by `-D warnings` | ||
= help: consider to implement From instead | ||
|
||
error: aborting due to previous error | ||
|
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
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
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.
That item might be quite large. We may want to reduce the span to the
impl
header.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.
How exactly would I extract the impl header? Didn't find a proper span while looking through documentation.
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.
It's OK, we can do this as a follow-up PR.
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.
Oke. If it's fine with you I could work on this, I might just need a hint on extracting the imp header Span.