Corrections for PR review.

This commit is contained in:
Darth-Revan 2019-07-15 18:43:47 +02:00 committed by flip1995
parent 0513202d25
commit f0dc97965a
No known key found for this signature in database
GPG key ID: 01C836B640FFDFB1
3 changed files with 31 additions and 17 deletions

View file

@ -17,7 +17,7 @@ declare_clippy_lint! {
/// ///
/// ** Example:** /// ** Example:**
/// ///
/// ```rust,ignore /// ```rust
/// // Bad /// // Bad
/// pub struct A; /// pub struct A;
/// ///
@ -26,6 +26,9 @@ declare_clippy_lint! {
/// "I am A".to_string() /// "I am A".to_string()
/// } /// }
/// } /// }
/// ```
///
/// ```rust
/// // Good /// // Good
/// use std::fmt; /// use std::fmt;
/// ///
@ -39,19 +42,19 @@ declare_clippy_lint! {
/// ``` /// ```
pub INHERENT_TO_STRING, pub INHERENT_TO_STRING,
style, style,
"type implements inherent method 'to_string()', but should instead implement the 'Display' trait" "type implements inherent method `to_string()`, but should instead implement the `Display` trait"
} }
declare_clippy_lint! { declare_clippy_lint! {
/// **What id does:** Checks for the definition of inherent methods with a signature of `to_string(&self) -> String` and if the type implementing this method also implements the `Display` trait. /// **What id does:** Checks for the definition of inherent methods with a signature of `to_string(&self) -> String` and if the type implementing this method also implements the `Display` trait.
/// ///
/// **Why is this bad?** This method is also implicitly defined if a type implements the 'Display' trait. The less versatile inherent method will then shadow the implementation introduced by `Display`. /// **Why is this bad?** This method is also implicitly defined if a type implements the `Display` trait. The less versatile inherent method will then shadow the implementation introduced by `Display`.
/// ///
/// **Known problems:** The inherent method will shadow the implementation by `Display`. If they behave differently, this may lead to confusing situations for users of the respective type. /// **Known problems:** None
/// ///
/// ** Example:** /// ** Example:**
/// ///
/// ```rust,ignore /// ```rust
/// // Bad /// // Bad
/// use std::fmt; /// use std::fmt;
/// ///
@ -68,6 +71,9 @@ declare_clippy_lint! {
/// write!(f, "I am A, too") /// write!(f, "I am A, too")
/// } /// }
/// } /// }
/// ```
///
/// ```rust
/// // Good /// // Good
/// use std::fmt; /// use std::fmt;
/// ///
@ -81,7 +87,7 @@ declare_clippy_lint! {
/// ``` /// ```
pub INHERENT_TO_STRING_SHADOW_DISPLAY, pub INHERENT_TO_STRING_SHADOW_DISPLAY,
correctness, correctness,
"type implements inherent method 'to_string()', which gets shadowed by the implementation of the 'Display' trait " "type implements inherent method `to_string()`, which gets shadowed by the implementation of the `Display` trait "
} }
declare_lint_pass!(InherentToString => [INHERENT_TO_STRING, INHERENT_TO_STRING_SHADOW_DISPLAY]); declare_lint_pass!(InherentToString => [INHERENT_TO_STRING, INHERENT_TO_STRING_SHADOW_DISPLAY]);
@ -114,7 +120,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InherentToString {
fn show_lint(cx: &LateContext<'_, '_>, item: &ImplItem) { fn show_lint(cx: &LateContext<'_, '_>, item: &ImplItem) {
let display_trait_id = let display_trait_id =
get_trait_def_id(cx, &["core", "fmt", "Display"]).expect("Failed to get trait ID of 'Display'!"); get_trait_def_id(cx, &["core", "fmt", "Display"]).expect("Failed to get trait ID of `Display`!");
// Get the real type of 'self' // Get the real type of 'self'
let fn_def_id = cx.tcx.hir().local_def_id(item.hir_id); let fn_def_id = cx.tcx.hir().local_def_id(item.hir_id);
@ -128,10 +134,10 @@ fn show_lint(cx: &LateContext<'_, '_>, item: &ImplItem) {
INHERENT_TO_STRING_SHADOW_DISPLAY, INHERENT_TO_STRING_SHADOW_DISPLAY,
item.span, item.span,
&format!( &format!(
"type '{}' implements inherent method 'to_string() -> String' which shadows the implementation of 'Display'", "type `{}` implements inherent method `to_string(&self) -> String` which shadows the implementation of `Display`",
self_type.to_string() self_type.to_string()
), ),
&format!("remove the inherent method from type '{}'", self_type.to_string()) &format!("remove the inherent method from type `{}`", self_type.to_string())
); );
} else { } else {
span_help_and_lint( span_help_and_lint(
@ -139,10 +145,10 @@ fn show_lint(cx: &LateContext<'_, '_>, item: &ImplItem) {
INHERENT_TO_STRING, INHERENT_TO_STRING,
item.span, item.span,
&format!( &format!(
"implementation of inherent method 'to_string() -> String' for type '{}'", "implementation of inherent method `to_string(&self) -> String` for type `{}`",
self_type.to_string() self_type.to_string()
), ),
&format!("implement trait 'Display' for type '{}' instead", self_type.to_string()), &format!("implement trait `Display` for type `{}` instead", self_type.to_string()),
); );
} }
} }

View file

@ -1,5 +1,5 @@
#![warn(clippy::inherent_to_string)] #![warn(clippy::inherent_to_string)]
//#![deny(clippy::inherent_to_string_shadow)] #![deny(clippy::inherent_to_string_shadow)]
use std::fmt; use std::fmt;

View file

@ -1,4 +1,4 @@
error: implementation of inherent method 'to_string() -> String' for type 'A' error: implementation of inherent method `to_string(&self) -> String` for type `A`
--> $DIR/inherent_to_string.rs:18:5 --> $DIR/inherent_to_string.rs:18:5
| |
LL | / fn to_string(&self) -> String { LL | / fn to_string(&self) -> String {
@ -7,9 +7,9 @@ LL | | }
| |_____^ | |_____^
| |
= note: `-D clippy::inherent-to-string` implied by `-D warnings` = note: `-D clippy::inherent-to-string` implied by `-D warnings`
= help: implement trait 'Display' for type 'A' instead = help: implement trait `Display` for type `A` instead
error: type 'C' implements inherent method 'to_string() -> String' which shadows the implementation of 'Display' error: type `C` implements inherent method `to_string(&self) -> String` which shadows the implementation of `Display`
--> $DIR/inherent_to_string.rs:42:5 --> $DIR/inherent_to_string.rs:42:5
| |
LL | / fn to_string(&self) -> String { LL | / fn to_string(&self) -> String {
@ -18,7 +18,15 @@ LL | | }
| |_____^ | |_____^
| |
= note: #[deny(clippy::inherent_to_string_shadow_display)] on by default = note: #[deny(clippy::inherent_to_string_shadow_display)] on by default
= help: remove the inherent method from type 'C' = help: remove the inherent method from type `C`
error: aborting due to 2 previous errors error: unknown clippy lint: clippy::inherent_to_string_shadow
--> $DIR/inherent_to_string.rs:2:9
|
LL | #![deny(clippy::inherent_to_string_shadow)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::unknown-clippy-lints` implied by `-D warnings`
error: aborting due to 3 previous errors