-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #12122 - andrewbanchich:tostring-impl, r=llogiq
add to_string_trait_impl lint closes #12076 changelog: [`to_string_trait_impl`]: add lint for direct `ToString` implementations
- Loading branch information
Showing
18 changed files
with
265 additions
and
133 deletions.
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
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,67 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use rustc_hir::{Impl, Item, ItemKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::declare_lint_pass; | ||
use rustc_span::sym; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for direct implementations of `ToString`. | ||
/// ### Why is this bad? | ||
/// This trait is automatically implemented for any type which implements the `Display` trait. | ||
/// As such, `ToString` shouldn’t be implemented directly: `Display` should be implemented instead, | ||
/// and you get the `ToString` implementation for free. | ||
/// ### Example | ||
/// ```no_run | ||
/// struct Point { | ||
/// x: usize, | ||
/// y: usize, | ||
/// } | ||
/// | ||
/// impl ToString for Point { | ||
/// fn to_string(&self) -> String { | ||
/// format!("({}, {})", self.x, self.y) | ||
/// } | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// struct Point { | ||
/// x: usize, | ||
/// y: usize, | ||
/// } | ||
/// | ||
/// impl std::fmt::Display for Point { | ||
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
/// write!(f, "({}, {})", self.x, self.y) | ||
/// } | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.77.0"] | ||
pub TO_STRING_TRAIT_IMPL, | ||
style, | ||
"check for direct implementations of `ToString`" | ||
} | ||
|
||
declare_lint_pass!(ToStringTraitImpl => [TO_STRING_TRAIT_IMPL]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for ToStringTraitImpl { | ||
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx Item<'tcx>) { | ||
if let ItemKind::Impl(Impl { | ||
of_trait: Some(trait_ref), | ||
.. | ||
}) = it.kind | ||
&& let Some(trait_did) = trait_ref.trait_def_id() | ||
&& cx.tcx.is_diagnostic_item(sym::ToString, trait_did) | ||
{ | ||
span_lint_and_help( | ||
cx, | ||
TO_STRING_TRAIT_IMPL, | ||
it.span, | ||
"direct implementation of `ToString`", | ||
None, | ||
"prefer implementing `Display` instead", | ||
); | ||
} | ||
} | ||
} |
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,31 @@ | ||
#![warn(clippy::to_string_trait_impl)] | ||
|
||
use std::fmt::{self, Display}; | ||
|
||
struct Point { | ||
x: usize, | ||
y: usize, | ||
} | ||
|
||
impl ToString for Point { | ||
fn to_string(&self) -> String { | ||
format!("({}, {})", self.x, self.y) | ||
} | ||
} | ||
|
||
struct Foo; | ||
|
||
impl Display for Foo { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "Foo") | ||
} | ||
} | ||
|
||
struct Bar; | ||
|
||
impl Bar { | ||
#[allow(clippy::inherent_to_string)] | ||
fn to_string(&self) -> String { | ||
String::from("Bar") | ||
} | ||
} |
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: direct implementation of `ToString` | ||
--> $DIR/to_string_trait_impl.rs:10:1 | ||
| | ||
LL | / impl ToString for Point { | ||
LL | | fn to_string(&self) -> String { | ||
LL | | format!("({}, {})", self.x, self.y) | ||
LL | | } | ||
LL | | } | ||
| |_^ | ||
| | ||
= help: prefer implementing `Display` instead | ||
= note: `-D clippy::to-string-trait-impl` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::to_string_trait_impl)]` | ||
|
||
error: aborting due to 1 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
Oops, something went wrong.