-
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
WIP: Add missing trait implementation lints #3752
Closed
Closed
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 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,130 @@ | ||
use crate::utils::span_lint; | ||
use rustc::hir::*; | ||
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; | ||
use rustc::{declare_tool_lint, lint_array}; | ||
use rustc::util::nodemap::NodeSet; | ||
|
||
macro_rules! missing_impl { | ||
($trait:ident, $trait_path:path, $lint_constant:ident, $struct_name:ident, | ||
$trait_check:ident) => ( | ||
declare_clippy_lint! { | ||
pub $lint_constant, | ||
correctness, | ||
concat!("detects missing implementations of ", stringify!($trait_path)) | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct $struct_name { | ||
impling_types: Option<NodeSet>, | ||
} | ||
|
||
impl $struct_name { | ||
pub fn new() -> $struct_name { | ||
$struct_name { impling_types: None } | ||
} | ||
} | ||
|
||
impl LintPass for $struct_name { | ||
fn name(&self) -> &'static str { | ||
stringify!($struct_name) | ||
} | ||
|
||
fn get_lints(&self) -> LintArray { | ||
lint_array!($lint_constant) | ||
} | ||
} | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for $struct_name { | ||
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) { | ||
if !cx.access_levels.is_reachable(item.id) { | ||
return; | ||
} | ||
|
||
match item.node { | ||
ItemKind::Struct(..) | | ||
ItemKind::Union(..) | | ||
ItemKind::Enum(..) => {} | ||
_ => return, | ||
} | ||
|
||
let x = match cx.tcx.lang_items().$trait_check() { | ||
Some(x) => x, | ||
None => return, | ||
}; | ||
|
||
if self.impling_types.is_none() { | ||
let mut impls = NodeSet::default(); | ||
cx.tcx.for_each_impl(x, |d| { | ||
if let Some(ty_def) = cx.tcx.type_of(d).ty_adt_def() { | ||
if let Some(node_id) = cx.tcx.hir().as_local_node_id(ty_def.did) { | ||
impls.insert(node_id); | ||
} | ||
} | ||
}); | ||
|
||
self.impling_types = Some(impls); | ||
} | ||
|
||
if !self.impling_types.as_ref().unwrap().contains(&item.id) { | ||
span_lint( | ||
cx, | ||
$lint_constant, | ||
item.span, | ||
concat!("type does not implement `", stringify!($trait_path), "`; \ | ||
consider adding #[derive(", stringify!($trait), ")] or a manual \ | ||
implementation")) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
|
||
/// **What it does:** Checks for `Copy` implementations missing from structs and enums | ||
/// | ||
/// **Why is this bad?** `Copy` is a core trait that should be implemented for | ||
/// all types as much as possible. | ||
/// | ||
/// For more, see the [Rust API Guidelines](https://rust-lang-nursery.github.io/api-guidelines/interoperability.html#c-common-traits) | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// // Bad | ||
/// struct Foo; | ||
/// | ||
/// // Good | ||
/// #[derive(Copy)] | ||
/// struct Bar; | ||
/// ``` | ||
missing_impl!( | ||
Copy, | ||
Copy, | ||
MISSING_COPY_IMPLEMENTATIONS, | ||
MissingCopyImplementations, | ||
copy_trait); | ||
|
||
/// **What it does:** Checks for `Debug` implementations missing from structs and enums | ||
/// | ||
/// **Why is this bad?** `Debug` is a core trait that should be implemented for | ||
/// all types as much as possible. | ||
/// | ||
/// For more, see the [Rust API Guidelines](https://rust-lang-nursery.github.io/api-guidelines/interoperability.html#c-common-traits) | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// // Bad | ||
/// struct Foo; | ||
/// | ||
/// // Good | ||
/// #[derive(Debug)] | ||
/// struct Bar; | ||
/// ``` | ||
missing_impl!( | ||
Debug, | ||
Debug, | ||
MISSING_DEBUG_IMPLEMENTATIONS, | ||
MissingDebugImplementations, | ||
debug_trait); |
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,36 @@ | ||
#![feature(untagged_unions)] | ||
#![allow(dead_code)] | ||
#![allow(clippy::all)] | ||
#![warn(clippy::missing_copy_implementations)] | ||
|
||
pub enum A {} | ||
|
||
#[derive(Clone, Copy)] | ||
pub enum B {} | ||
|
||
pub enum C {} | ||
|
||
impl Copy for C {} | ||
impl Clone for C { | ||
fn clone(&self) -> C { *self } | ||
} | ||
|
||
pub struct Foo; | ||
|
||
#[derive(Clone, Copy)] | ||
pub struct Bar; | ||
|
||
pub struct Baz; | ||
|
||
impl Copy for Baz {} | ||
impl Clone for Baz { | ||
fn clone(&self) -> Baz { *self } | ||
} | ||
|
||
struct PrivateStruct; | ||
|
||
enum PrivateEnum {} | ||
|
||
struct GenericType<T>(T); | ||
|
||
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,16 @@ | ||
error: type does not implement `Copy`; consider adding #[derive(Copy)] or a manual implementation | ||
--> $DIR/missing_traits_copy.rs:6:1 | ||
| | ||
LL | pub enum A {} | ||
| ^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::missing-copy-implementations` implied by `-D warnings` | ||
|
||
error: type does not implement `Copy`; consider adding #[derive(Copy)] or a manual implementation | ||
--> $DIR/missing_traits_copy.rs:18:1 | ||
| | ||
LL | pub struct Foo; | ||
| ^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
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,38 @@ | ||
#![feature(untagged_unions)] | ||
#![allow(dead_code)] | ||
#![allow(clippy::all)] | ||
#![warn(clippy::missing_debug_implementations)] | ||
|
||
pub enum A {} | ||
|
||
#[derive(Debug)] | ||
pub enum B {} | ||
|
||
pub enum C {} | ||
|
||
impl std::fmt::Debug for C { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
f.debug_struct("C").finish() | ||
} | ||
} | ||
|
||
pub struct Foo; | ||
|
||
#[derive(Debug)] | ||
pub struct Bar; | ||
|
||
pub struct Baz; | ||
|
||
impl std::fmt::Debug for Baz { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
f.debug_struct("Baz").finish() | ||
} | ||
} | ||
|
||
struct PrivateStruct; | ||
|
||
enum PrivateEnum {} | ||
|
||
struct GenericType<T>(T); | ||
|
||
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,16 @@ | ||
error: type does not implement `Debug`; consider adding #[derive(Debug)] or a manual implementation | ||
--> $DIR/missing_traits_debug.rs:6:1 | ||
| | ||
LL | pub enum A {} | ||
| ^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::missing-debug-implementations` implied by `-D warnings` | ||
|
||
error: type does not implement `Debug`; consider adding #[derive(Debug)] or a manual implementation | ||
--> $DIR/missing_traits_debug.rs:19:1 | ||
| | ||
LL | pub struct Foo; | ||
| ^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
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.
This is where you give a lint its category