Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
b587871
Simplify `get_enclosing_loop_or_multi_call_closure`
Jarcho Nov 15, 2023
cdc4c69
Make `expr_use_ctxt` always return `Some` unless the syntax context c…
Jarcho Nov 15, 2023
57dd25e
FP: `needless_return_with_question_mark` with implicit Error Conversion
PartiallyUntyped Dec 26, 2023
090c228
Fix/Issue11932: assert* in multi-condition after unrolling will cause…
cocodery Jan 3, 2024
b5a2192
fix: incorrect suggestions generated by `manual_retain` lint
yuxqiu Jan 3, 2024
03b3a16
test: add more test cases
yuxqiu Jan 4, 2024
bd6e920
modify check that any macros will be ingored in this lint, and add test
cocodery Jan 6, 2024
e0228ee
Fixes FP in `redundant_closure_call` when closures are passed to macros
PartiallyUntyped Jan 3, 2024
3b8f62f
Add new `unnecessary_result_map_or_else` lint
GuillaumeGomez Jan 18, 2024
32bbeba
Add ui test for `unnecessary_result_map_or_else`
GuillaumeGomez Jan 18, 2024
e86da9e
Update CHANGELOG
GuillaumeGomez Jan 18, 2024
0b6cf3b
We don't look into static items anymore during const prop
oli-obk Jan 8, 2024
99d8d33
remove StructuralEq trait
RalfJung Sep 26, 2023
9cbc582
Rename the unescaping functions.
nnethercote Jan 24, 2024
e456c28
Don't warn about modulo arithmetic when comparing to zero
mdm Jan 19, 2024
58de630
Remove an unused error count check
oli-obk Jan 25, 2024
798865c
Merge commit '66c29b973b3b10278bd39f4e26b08522a379c2c9' into clippy-s…
flip1995 Jan 25, 2024
42d13f8
[`unconditional_recursion`]: compare by types instead of `DefId`s
y21 Jan 20, 2024
87a6300
add a test for rust-lang/rust-clippy#12181
y21 Jan 21, 2024
fd3e966
avoid linting on `#[track_caller]` functions in `redundant_closure`
y21 Jan 25, 2024
a65fe78
Auto merge of #116167 - RalfJung:structural-eq, r=lcnr
bors Jan 26, 2024
ed74c22
Auto merge of #12202 - y21:issue12199, r=Jarcho
bors Jan 26, 2024
9ce0b83
Clippy: Fix empty suggestion in from_over_into
flip1995 Jan 26, 2024
14e1520
Warn if an item coming from more recent version than MSRV is used
GuillaumeGomez Jan 17, 2024
8de9d8c
Auto merge of #12160 - GuillaumeGomez:incompatible-msrv, r=blyxyas
bors Jan 26, 2024
f096e91
Rollup merge of #120329 - nnethercote:3349-precursors, r=fee1-dead
matthiaskrgr Jan 26, 2024
57f63a3
Rollup merge of #120345 - flip1995:clippy-subtree-update, r=Manishearth
matthiaskrgr Jan 26, 2024
1d94cc3
remove illegal_floating_point_literal_pattern lint
RalfJung Jan 6, 2024
f58950d
correct lint case
PartiallyUntyped Jan 26, 2024
6d76d14
add to_string_trait_impl lint
andrewbanchich Jan 10, 2024
8905f78
Auto merge of #12082 - PartiallyTyped:1553, r=dswij
bors Jan 27, 2024
855aa08
Auto merge of #12178 - mdm:modulo-arithmetic-comparison-to-zero, r=ll…
bors Jan 27, 2024
79f10cf
Auto merge of #12122 - andrewbanchich:tostring-impl, r=llogiq
bors Jan 27, 2024
85e08cd
Auto merge of #12169 - GuillaumeGomez:unnecessary_result_map_or_else,…
bors Jan 27, 2024
a51fc2a
Fix `NonZero` clippy lints.
reitermarkus May 12, 2023
ff5afac
[`never_loop`]: recognize `?` desugaring in try blocks
y21 Jan 27, 2024
18e1f25
Auto merge of #12206 - y21:issue12205, r=Alexendoo
bors Jan 27, 2024
276ce39
Auto merge of #12083 - cocodery:fix/issue11932, r=Alexendoo
bors Jan 27, 2024
8ccf6a6
Auto merge of #12084 - yuxqiu:manual_retain, r=Alexendoo
bors Jan 27, 2024
3aa2c27
rewrote to match only Result::err cons
PartiallyUntyped Jan 28, 2024
e7a3cb7
Auto merge of #12021 - PartiallyTyped:11982, r=flip1995
bors Jan 29, 2024
b3d5377
Make `redundant_closure_for_method_calls` suggest relative paths
modelflat Aug 21, 2023
b2f2080
Add regression test for #2371
GuillaumeGomez Jan 29, 2024
ce8b4b6
Remove fixed FIXME
GuillaumeGomez Jan 29, 2024
3cd713a
Auto merge of #11370 - modelflat:suggest-relpath-in-redundant-closure…
bors Jan 29, 2024
73706e8
Makes clippy-driver check for --sysroot in arg files
Jan 26, 2024
1156375
Auto merge of #12203 - daivinhtran:fix-clippy-driver-to-accept-param-…
bors Jan 29, 2024
455c07b
Auto merge of #12210 - GuillaumeGomez:add-regression-test-2371, r=bly…
bors Jan 29, 2024
d02df12
add configuration for [`wildcard_imports`] to ignore certain imports
J-ZhengLi Dec 18, 2023
314bdde
add more test cases & improve docs & replace `Vec` with `FxHashSet` f…
J-ZhengLi Jan 29, 2024
c0f49a9
hir: Simplify `hir_owner_nodes` query
petrochenkov Jan 25, 2024
7539054
hir: Add non-optional `hir_owner_nodes` for real `OwnerId`s
petrochenkov Jan 25, 2024
233c8c9
hir: Remove `hir::Map::{owner,expect_owner}`
petrochenkov Jan 25, 2024
f0dbf68
Rollup merge of #120342 - oli-obk:track_errors6, r=nnethercote
GuillaumeGomez Jan 30, 2024
3106219
Don't lint slice type annotations for byte strings
bpandreotti Jan 30, 2024
ae0f0fd
Don't hash lints differently to non-lints.
nnethercote Jan 30, 2024
9e3bb89
Auto merge of #120346 - petrochenkov:ownodes, r=oli-obk
bors Jan 31, 2024
7836678
Rollup merge of #120490 - nnethercote:Diagnostic-hashing, r=estebank
Nadrieril Jan 31, 2024
6619e8c
Add `lint_groups_priority` lint
Alexendoo Jan 31, 2024
b58b88c
Auto merge of #11832 - Alexendoo:lint-groups-priority, r=flip1995
bors Jan 31, 2024
fe8c2e2
Fixed FP in `unused_io_amount` for `Ok(lit)`, `unrachable!`
PartiallyUntyped Jan 30, 2024
46dd826
rename conf option to `allowed_wildcard_imports`
J-ZhengLi Feb 2, 2024
9b6f866
Auto merge of #12217 - PartiallyTyped:12208, r=blyxyas
bors Feb 2, 2024
c82162e
Auto merge of #11979 - J-ZhengLi:issue11428, r=Alexendoo
bors Feb 2, 2024
abced20
Avoid deleting labeled blocks
sanxiyn Jan 31, 2024
9fb4107
Auto merge of #12219 - sanxiyn:labeled-block, r=blyxyas
bors Feb 3, 2024
be47e32
Update version attributes for 1.76 lints
xFrednet Feb 4, 2024
aa1de4d
Changelog for Clippy 1.76 :cat2:
xFrednet Feb 4, 2024
a3baebc
Add ref_as_ptr lint
marcin-serwin Jan 2, 2024
34e4c9f
Auto merge of #12087 - marcin-serwin:ref_as_ptr_cast, r=blyxyas
bors Feb 4, 2024
7c3908f
[`redundant_locals`]: take by-value closure captures into account
y21 Feb 4, 2024
8baeb26
Auto merge of #12224 - xFrednet:chaneglog-1-76, r=Manishearth
bors Feb 5, 2024
d13ce19
Rollup merge of #116284 - RalfJung:no-nan-match, r=cjgillot
matthiaskrgr Feb 5, 2024
6807977
also check for coroutines
y21 Feb 5, 2024
7f80b44
new lint: `manual_c_str_literals`
y21 Dec 3, 2023
005b6c2
Auto merge of #11919 - y21:manual_c_str_literals, r=xFrednet
bors Feb 5, 2024
fdf819d
Auto merge of #12227 - y21:issue12225, r=Manishearth
bors Feb 5, 2024
7895b98
Add CoroutineClosure to TyKind, AggregateKind, UpvarArgs
compiler-errors Jan 24, 2024
36f7248
Fix release year in CHANGELOG.md
nyurik Feb 6, 2024
d910f77
Auto merge of #12235 - nyurik:patch-1, r=flip1995
bors Feb 6, 2024
b1e5a58
Auto merge of #11812 - Jarcho:issue_11786, r=Alexendoo
bors Feb 6, 2024
08c8cd5
Auto merge of #12216 - bpandreotti:redundant-type-annotations-fix, r=…
bors Feb 7, 2024
62dcbd6
Auto merge of #12177 - y21:issue12154, r=Jarcho
bors Feb 7, 2024
d2f76f7
Merge remote-tracking branch 'upstream/master' into rustup
flip1995 Feb 8, 2024
031c46d
Bump nightly version -> 2024-02-08
flip1995 Feb 8, 2024
2ca6c84
Bump Clippy version -> 0.1.78
flip1995 Feb 8, 2024
60cb29c
Auto merge of #12246 - flip1995:rustup, r=flip1995
bors Feb 8, 2024
beb4f7d
Merge commit '60cb29c5e4f9772685c9873752196725c946a849' into clippyup
flip1995 Feb 8, 2024
4ec9eec
Update Cargo.lock
flip1995 Feb 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add to_string_trait_impl lint
  • Loading branch information
andrewbanchich committed Jan 27, 2024
commit 6d76d145653bbe05e3ac49ea12626f52a1582970
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5623,6 +5623,7 @@ Released 2018-09-13
[`to_digit_is_some`]: https://rust-lang.github.io/rust-clippy/master/index.html#to_digit_is_some
[`to_string_in_display`]: https://rust-lang.github.io/rust-clippy/master/index.html#to_string_in_display
[`to_string_in_format_args`]: https://rust-lang.github.io/rust-clippy/master/index.html#to_string_in_format_args
[`to_string_trait_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#to_string_trait_impl
[`todo`]: https://rust-lang.github.io/rust-clippy/master/index.html#todo
[`too_many_arguments`]: https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
[`too_many_lines`]: https://rust-lang.github.io/rust-clippy/master/index.html#too_many_lines
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::tests_outside_test_module::TESTS_OUTSIDE_TEST_MODULE_INFO,
crate::thread_local_initializer_can_be_made_const::THREAD_LOCAL_INITIALIZER_CAN_BE_MADE_CONST_INFO,
crate::to_digit_is_some::TO_DIGIT_IS_SOME_INFO,
crate::to_string_trait_impl::TO_STRING_TRAIT_IMPL_INFO,
crate::trailing_empty_array::TRAILING_EMPTY_ARRAY_INFO,
crate::trait_bounds::TRAIT_DUPLICATION_IN_BOUNDS_INFO,
crate::trait_bounds::TYPE_REPETITION_IN_BOUNDS_INFO,
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ mod temporary_assignment;
mod tests_outside_test_module;
mod thread_local_initializer_can_be_made_const;
mod to_digit_is_some;
mod to_string_trait_impl;
mod trailing_empty_array;
mod trait_bounds;
mod transmute;
Expand Down Expand Up @@ -1097,6 +1098,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
Box::new(thread_local_initializer_can_be_made_const::ThreadLocalInitializerCanBeMadeConst::new(msrv()))
});
store.register_late_pass(move |_| Box::new(incompatible_msrv::IncompatibleMsrv::new(msrv())));
store.register_late_pass(|_| Box::new(to_string_trait_impl::ToStringTraitImpl));
// add lints here, do not remove this comment, it's used in `new_lint`
}

Expand Down
15 changes: 8 additions & 7 deletions clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rustc_session::declare_lint_pass;
use rustc_span::def_id::LocalDefId;
use rustc_span::{BytePos, Pos, Span};
use std::borrow::Cow;
use std::fmt::Display;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -146,14 +147,14 @@ impl<'tcx> RetReplacement<'tcx> {
}
}

impl<'tcx> ToString for RetReplacement<'tcx> {
fn to_string(&self) -> String {
impl<'tcx> Display for RetReplacement<'tcx> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Empty => String::new(),
Self::Block => "{}".to_string(),
Self::Unit => "()".to_string(),
Self::IfSequence(inner, _) => format!("({inner})"),
Self::Expr(inner, _) => inner.to_string(),
Self::Empty => write!(f, ""),
Self::Block => write!(f, "{{}}"),
Self::Unit => write!(f, "()"),
Self::IfSequence(inner, _) => write!(f, "({inner})"),
Self::Expr(inner, _) => write!(f, "{inner}"),
}
}
}
Expand Down
67 changes: 67 additions & 0 deletions clippy_lints/src/to_string_trait_impl.rs
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",
);
}
}
}
1 change: 1 addition & 0 deletions tests/ui/format_args.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::panic::Location;

struct Somewhere;

#[allow(clippy::to_string_trait_impl)]
impl ToString for Somewhere {
fn to_string(&self) -> String {
String::from("somewhere")
Expand Down
1 change: 1 addition & 0 deletions tests/ui/format_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::panic::Location;

struct Somewhere;

#[allow(clippy::to_string_trait_impl)]
impl ToString for Somewhere {
fn to_string(&self) -> String {
String::from("somewhere")
Expand Down
50 changes: 25 additions & 25 deletions tests/ui/format_args.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: `to_string` applied to a type that implements `Display` in `format!` args
--> $DIR/format_args.rs:76:72
--> $DIR/format_args.rs:77:72
|
LL | let _ = format!("error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this
Expand All @@ -8,145 +8,145 @@ LL | let _ = format!("error: something failed at {}", Location::caller().to_
= help: to override `-D warnings` add `#[allow(clippy::to_string_in_format_args)]`

error: `to_string` applied to a type that implements `Display` in `write!` args
--> $DIR/format_args.rs:80:27
--> $DIR/format_args.rs:81:27
|
LL | Location::caller().to_string()
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `writeln!` args
--> $DIR/format_args.rs:85:27
--> $DIR/format_args.rs:86:27
|
LL | Location::caller().to_string()
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `print!` args
--> $DIR/format_args.rs:87:63
--> $DIR/format_args.rs:88:63
|
LL | print!("error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:88:65
--> $DIR/format_args.rs:89:65
|
LL | println!("error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `eprint!` args
--> $DIR/format_args.rs:89:64
--> $DIR/format_args.rs:90:64
|
LL | eprint!("error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `eprintln!` args
--> $DIR/format_args.rs:90:66
--> $DIR/format_args.rs:91:66
|
LL | eprintln!("error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `format_args!` args
--> $DIR/format_args.rs:91:77
--> $DIR/format_args.rs:92:77
|
LL | let _ = format_args!("error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `assert!` args
--> $DIR/format_args.rs:92:70
--> $DIR/format_args.rs:93:70
|
LL | assert!(true, "error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `assert_eq!` args
--> $DIR/format_args.rs:93:73
--> $DIR/format_args.rs:94:73
|
LL | assert_eq!(0, 0, "error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `assert_ne!` args
--> $DIR/format_args.rs:94:73
--> $DIR/format_args.rs:95:73
|
LL | assert_ne!(0, 0, "error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `panic!` args
--> $DIR/format_args.rs:95:63
--> $DIR/format_args.rs:96:63
|
LL | panic!("error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:96:20
--> $DIR/format_args.rs:97:20
|
LL | println!("{}", X(1).to_string());
| ^^^^^^^^^^^^^^^^ help: use this: `*X(1)`

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:97:20
--> $DIR/format_args.rs:98:20
|
LL | println!("{}", Y(&X(1)).to_string());
| ^^^^^^^^^^^^^^^^^^^^ help: use this: `***Y(&X(1))`

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:98:24
--> $DIR/format_args.rs:99:24
|
LL | println!("{}", Z(1).to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:99:20
--> $DIR/format_args.rs:100:20
|
LL | println!("{}", x.to_string());
| ^^^^^^^^^^^^^ help: use this: `**x`

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:100:20
--> $DIR/format_args.rs:101:20
|
LL | println!("{}", x_ref.to_string());
| ^^^^^^^^^^^^^^^^^ help: use this: `***x_ref`

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:102:39
--> $DIR/format_args.rs:103:39
|
LL | println!("{foo}{bar}", foo = "foo".to_string(), bar = "bar");
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:103:52
--> $DIR/format_args.rs:104:52
|
LL | println!("{foo}{bar}", foo = "foo", bar = "bar".to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:104:39
--> $DIR/format_args.rs:105:39
|
LL | println!("{foo}{bar}", bar = "bar".to_string(), foo = "foo");
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:105:52
--> $DIR/format_args.rs:106:52
|
LL | println!("{foo}{bar}", bar = "bar", foo = "foo".to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `print!` args
--> $DIR/format_args.rs:117:37
--> $DIR/format_args.rs:118:37
|
LL | print!("{}", (Location::caller().to_string()));
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `print!` args
--> $DIR/format_args.rs:118:39
--> $DIR/format_args.rs:119:39
|
LL | print!("{}", ((Location::caller()).to_string()));
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `format!` args
--> $DIR/format_args.rs:146:38
--> $DIR/format_args.rs:147:38
|
LL | let x = format!("{} {}", a, b.to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:160:24
--> $DIR/format_args.rs:161:24
|
LL | println!("{}", original[..10].to_string());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use this: `&original[..10]`
Expand Down
31 changes: 31 additions & 0 deletions tests/ui/to_string_trait_impl.rs
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")
}
}
16 changes: 16 additions & 0 deletions tests/ui/to_string_trait_impl.stderr
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

3 changes: 3 additions & 0 deletions tests/ui/unconditional_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ impl PartialEq for S8 {

struct S9;

#[allow(clippy::to_string_trait_impl)]
impl std::string::ToString for S9 {
fn to_string(&self) -> String {
//~^ ERROR: function cannot return without recursing
Expand All @@ -215,6 +216,7 @@ impl std::string::ToString for S9 {

struct S10;

#[allow(clippy::to_string_trait_impl)]
impl std::string::ToString for S10 {
fn to_string(&self) -> String {
//~^ ERROR: function cannot return without recursing
Expand All @@ -225,6 +227,7 @@ impl std::string::ToString for S10 {

struct S11;

#[allow(clippy::to_string_trait_impl)]
impl std::string::ToString for S11 {
fn to_string(&self) -> String {
//~^ ERROR: function cannot return without recursing
Expand Down
Loading