From 46ce17d4a043f53986ad330dc17cb7d735ab35cd Mon Sep 17 00:00:00 2001 From: Marcin Serwin Date: Wed, 6 May 2020 09:50:34 +0200 Subject: [PATCH 1/9] Prepare new lint files --- CHANGELOG.md | 1 + clippy_lints/src/lib.rs | 3 +++ clippy_lints/src/no_to_string_in_display.rs | 28 +++++++++++++++++++++ src/lintlist/mod.rs | 7 ++++++ tests/ui/no_to_string_in_display.rs | 5 ++++ 5 files changed, 44 insertions(+) create mode 100644 clippy_lints/src/no_to_string_in_display.rs create mode 100644 tests/ui/no_to_string_in_display.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index facf363e371e..7c45c72afdef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1487,6 +1487,7 @@ Released 2018-09-13 [`new_ret_no_self`]: https://rust-lang.github.io/rust-clippy/master/index.html#new_ret_no_self [`new_without_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default [`no_effect`]: https://rust-lang.github.io/rust-clippy/master/index.html#no_effect +[`no_to_string_in_display`]: https://rust-lang.github.io/rust-clippy/master/index.html#no_to_string_in_display [`non_ascii_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_ascii_literal [`nonminimal_bool`]: https://rust-lang.github.io/rust-clippy/master/index.html#nonminimal_bool [`nonsensical_open_options`]: https://rust-lang.github.io/rust-clippy/master/index.html#nonsensical_open_options diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 97785cba8835..3e1808b9747c 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -279,6 +279,7 @@ mod neg_cmp_op_on_partial_ord; mod neg_multiply; mod new_without_default; mod no_effect; +mod no_to_string_in_display; mod non_copy_const; mod non_expressive_names; mod open_options; @@ -744,6 +745,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: &new_without_default::NEW_WITHOUT_DEFAULT, &no_effect::NO_EFFECT, &no_effect::UNNECESSARY_OPERATION, + &no_to_string_in_display::NO_TO_STRING_IN_DISPLAY, &non_copy_const::BORROW_INTERIOR_MUTABLE_CONST, &non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST, &non_expressive_names::JUST_UNDERSCORES_AND_DIGITS, @@ -1157,6 +1159,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&mut_mut::MUT_MUT), LintId::of(&needless_continue::NEEDLESS_CONTINUE), LintId::of(&needless_pass_by_value::NEEDLESS_PASS_BY_VALUE), + LintId::of(&no_to_string_in_display::NO_TO_STRING_IN_DISPLAY), LintId::of(&non_expressive_names::SIMILAR_NAMES), LintId::of(&ranges::RANGE_PLUS_ONE), LintId::of(&shadow::SHADOW_UNRELATED), diff --git a/clippy_lints/src/no_to_string_in_display.rs b/clippy_lints/src/no_to_string_in_display.rs new file mode 100644 index 000000000000..f1941f2aa6b8 --- /dev/null +++ b/clippy_lints/src/no_to_string_in_display.rs @@ -0,0 +1,28 @@ +use rustc_lint::{LateLintPass, LateContext}; +use rustc_session::{declare_lint_pass, declare_tool_lint}; +use rustc_hir::*; + +declare_clippy_lint! { + /// **What it does:** + /// + /// **Why is this bad?** + /// + /// **Known problems:** None. + /// + /// **Example:** + /// + /// ```rust + /// // example code where clippy issues a warning + /// ``` + /// Use instead: + /// ```rust + /// // example code which does not raise clippy warning + /// ``` + pub NO_TO_STRING_IN_DISPLAY, + correctness, + "default lint description" +} + +declare_lint_pass!(NoToStringInDisplay => [NO_TO_STRING_IN_DISPLAY]); + +impl LateLintPass<'_, '_> for NoToStringInDisplay {} diff --git a/src/lintlist/mod.rs b/src/lintlist/mod.rs index a7685f67211f..e09ce1005aea 100644 --- a/src/lintlist/mod.rs +++ b/src/lintlist/mod.rs @@ -1529,6 +1529,13 @@ pub static ref ALL_LINTS: Vec = vec![ deprecation: None, module: "no_effect", }, + Lint { + name: "no_to_string_in_display", + group: "correctness", + desc: "default lint description", + deprecation: None, + module: "no_to_string_in_display", + }, Lint { name: "non_ascii_literal", group: "pedantic", diff --git a/tests/ui/no_to_string_in_display.rs b/tests/ui/no_to_string_in_display.rs new file mode 100644 index 000000000000..d6456772c98a --- /dev/null +++ b/tests/ui/no_to_string_in_display.rs @@ -0,0 +1,5 @@ +#![warn(clippy::no_to_string_in_display)] + +fn main() { + // test code goes here +} From 481468e3dfb25052f33502d0ea43356712ad438b Mon Sep 17 00:00:00 2001 From: Marcin Serwin Date: Wed, 6 May 2020 10:16:29 +0200 Subject: [PATCH 2/9] Fill in lint description --- clippy_lints/src/no_to_string_in_display.rs | 22 ++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/no_to_string_in_display.rs b/clippy_lints/src/no_to_string_in_display.rs index f1941f2aa6b8..341b32b80701 100644 --- a/clippy_lints/src/no_to_string_in_display.rs +++ b/clippy_lints/src/no_to_string_in_display.rs @@ -3,24 +3,36 @@ use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_hir::*; declare_clippy_lint! { - /// **What it does:** + /// **What it does:** Checks for uses of `to_string()` when implementing + /// `Display` traits. /// - /// **Why is this bad?** + /// **Why is this bad?** Usually `to_string` is implemented indirectly + /// via `Display`. Hence using it while implementing `Display` would + /// lead to infinite recursion. /// /// **Known problems:** None. /// /// **Example:** /// /// ```rust - /// // example code where clippy issues a warning + /// impl fmt::Display for Structure { + /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + /// write!(f, "{}", self.to_string()) + /// } + /// } + /// /// ``` /// Use instead: /// ```rust - /// // example code which does not raise clippy warning + /// impl fmt::Display for Structure { + /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + /// write!(f, "{}", self.0) + /// } + /// } /// ``` pub NO_TO_STRING_IN_DISPLAY, correctness, - "default lint description" + "to_string method used while implementing Display trait" } declare_lint_pass!(NoToStringInDisplay => [NO_TO_STRING_IN_DISPLAY]); From 573c99fbb4d8c70f2a7cc525e836ae8caf3f7acd Mon Sep 17 00:00:00 2001 From: Marcin Serwin Date: Wed, 6 May 2020 13:22:22 +0200 Subject: [PATCH 3/9] Register lint pass --- clippy_lints/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 3e1808b9747c..e8fbf369782b 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -1019,6 +1019,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_early_pass(|| box reference::DerefAddrOf); store.register_early_pass(|| box reference::RefInDeref); store.register_early_pass(|| box double_parens::DoubleParens); + store.register_late_pass(|| box no_to_string_in_display::NoToStringInDisplay); store.register_early_pass(|| box unsafe_removed_from_name::UnsafeNameRemoval); store.register_early_pass(|| box if_not_else::IfNotElse); store.register_early_pass(|| box else_if_without_else::ElseIfWithoutElse); From 13502bf5018823d850301896408f29579708c3d0 Mon Sep 17 00:00:00 2001 From: Marcin Serwin Date: Wed, 6 May 2020 14:01:27 +0200 Subject: [PATCH 4/9] Add no_to_string_in_display test --- tests/ui/no_to_string_in_display.rs | 34 ++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/tests/ui/no_to_string_in_display.rs b/tests/ui/no_to_string_in_display.rs index d6456772c98a..3dbe02e854a4 100644 --- a/tests/ui/no_to_string_in_display.rs +++ b/tests/ui/no_to_string_in_display.rs @@ -1,5 +1,37 @@ #![warn(clippy::no_to_string_in_display)] +use std::fmt; + +struct A; +impl A { + fn fmt(&self) { + self.to_string(); + } +} + +trait B { + fn fmt(&self) {} +} + +impl B for A { + fn fmt(&self) { + self.to_string(); + } +} + +impl fmt::Display for A { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.to_string()) + } +} + +fn fmt(a: A) { + a.to_string(); +} + fn main() { - // test code goes here + let a = A; + a.to_string(); + a.fmt(); + fmt(a); } From ac834e168d75d172379793ccc08ad62820ff056f Mon Sep 17 00:00:00 2001 From: Marcin Serwin Date: Wed, 6 May 2020 17:07:07 +0200 Subject: [PATCH 5/9] Implement no to_string_in_display --- clippy_lints/src/no_to_string_in_display.rs | 37 +++++++++++++++++++-- tests/ui/no_to_string_in_display.stderr | 10 ++++++ 2 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 tests/ui/no_to_string_in_display.stderr diff --git a/clippy_lints/src/no_to_string_in_display.rs b/clippy_lints/src/no_to_string_in_display.rs index 341b32b80701..f75e52ff14d5 100644 --- a/clippy_lints/src/no_to_string_in_display.rs +++ b/clippy_lints/src/no_to_string_in_display.rs @@ -1,6 +1,8 @@ -use rustc_lint::{LateLintPass, LateContext}; -use rustc_session::{declare_lint_pass, declare_tool_lint}; +use crate::utils::{match_def_path, paths, span_lint}; +use if_chain::if_chain; use rustc_hir::*; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_session::{declare_lint_pass, declare_tool_lint}; declare_clippy_lint! { /// **What it does:** Checks for uses of `to_string()` when implementing @@ -37,4 +39,33 @@ declare_clippy_lint! { declare_lint_pass!(NoToStringInDisplay => [NO_TO_STRING_IN_DISPLAY]); -impl LateLintPass<'_, '_> for NoToStringInDisplay {} +impl LateLintPass<'_, '_> for NoToStringInDisplay { + fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &Expr<'_>) { + let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id); + let parent_node = cx.tcx.hir().find(parent_id); + + if_chain! { + if let ExprKind::MethodCall(ref path, _, _) = expr.kind; + if path.ident.as_str() == "to_string"; + if let Some(Node::ImplItem(ImplItem {ident, kind, ..})) = parent_node; + if ident.as_str() == "fmt"; + if let ImplItemKind::Fn(FnSig {decl, ..}, _) = kind; + if let FnDecl{inputs, ..} = decl; + if let Ty {kind, ..} = &inputs[0]; + if let TyKind::Rptr(_, MutTy {ty, ..}) = kind; + if let Ty {kind, ..} = ty; + if let TyKind::Path(QPath::Resolved(_, path)) = kind; + if let Path{res, ..} = path; + if let def::Res::SelfTy(Some(did), _) = res; + if match_def_path(cx, *did, &paths::DISPLAY_TRAIT); + then { + span_lint( + cx, + NO_TO_STRING_IN_DISPLAY, + expr.span, + "Using to_string in fmt::Display implementation might lead to infinite recursion", + ); + } + } + } +} diff --git a/tests/ui/no_to_string_in_display.stderr b/tests/ui/no_to_string_in_display.stderr new file mode 100644 index 000000000000..a6c20b3a93ff --- /dev/null +++ b/tests/ui/no_to_string_in_display.stderr @@ -0,0 +1,10 @@ +error: Using to_string in fmt::Display implementation might lead to infinite recursion + --> $DIR/no_to_string_in_display.rs:24:25 + | +LL | write!(f, "{}", self.to_string()) + | ^^^^^^^^^^^^^^^^ + | + = note: `-D clippy::no-to-string-in-display` implied by `-D warnings` + +error: aborting due to previous error + From a24eb38dbd8ab432583dc19cf8479591b21e44b1 Mon Sep 17 00:00:00 2001 From: Marcin Serwin Date: Wed, 6 May 2020 17:09:29 +0200 Subject: [PATCH 6/9] Update lints --- clippy_lints/src/lib.rs | 3 ++- src/lintlist/mod.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index e8fbf369782b..95d9d9bb4e58 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -1160,7 +1160,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&mut_mut::MUT_MUT), LintId::of(&needless_continue::NEEDLESS_CONTINUE), LintId::of(&needless_pass_by_value::NEEDLESS_PASS_BY_VALUE), - LintId::of(&no_to_string_in_display::NO_TO_STRING_IN_DISPLAY), LintId::of(&non_expressive_names::SIMILAR_NAMES), LintId::of(&ranges::RANGE_PLUS_ONE), LintId::of(&shadow::SHADOW_UNRELATED), @@ -1366,6 +1365,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&new_without_default::NEW_WITHOUT_DEFAULT), LintId::of(&no_effect::NO_EFFECT), LintId::of(&no_effect::UNNECESSARY_OPERATION), + LintId::of(&no_to_string_in_display::NO_TO_STRING_IN_DISPLAY), LintId::of(&non_copy_const::BORROW_INTERIOR_MUTABLE_CONST), LintId::of(&non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST), LintId::of(&non_expressive_names::JUST_UNDERSCORES_AND_DIGITS), @@ -1668,6 +1668,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&misc::FLOAT_CMP), LintId::of(&misc::MODULO_ONE), LintId::of(&mut_key::MUTABLE_KEY_TYPE), + LintId::of(&no_to_string_in_display::NO_TO_STRING_IN_DISPLAY), LintId::of(&non_copy_const::BORROW_INTERIOR_MUTABLE_CONST), LintId::of(&non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST), LintId::of(&open_options::NONSENSICAL_OPEN_OPTIONS), diff --git a/src/lintlist/mod.rs b/src/lintlist/mod.rs index e09ce1005aea..20bdd710b6cf 100644 --- a/src/lintlist/mod.rs +++ b/src/lintlist/mod.rs @@ -1532,7 +1532,7 @@ pub static ref ALL_LINTS: Vec = vec![ Lint { name: "no_to_string_in_display", group: "correctness", - desc: "default lint description", + desc: "to_string method used while implementing Display trait", deprecation: None, module: "no_to_string_in_display", }, From fef175c4995ed38958b49342f40325b8c29b9b86 Mon Sep 17 00:00:00 2001 From: Marcin Serwin Date: Wed, 6 May 2020 17:15:20 +0200 Subject: [PATCH 7/9] Remove wildcard import --- clippy_lints/src/no_to_string_in_display.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/no_to_string_in_display.rs b/clippy_lints/src/no_to_string_in_display.rs index f75e52ff14d5..cdd050b5480e 100644 --- a/clippy_lints/src/no_to_string_in_display.rs +++ b/clippy_lints/src/no_to_string_in_display.rs @@ -1,6 +1,6 @@ use crate::utils::{match_def_path, paths, span_lint}; use if_chain::if_chain; -use rustc_hir::*; +use rustc_hir::{Expr, ExprKind, FnDecl, FnSig, ImplItem, ImplItemKind, MutTy, Node, Path, QPath, Ty, TyKind, def}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; From de1f0678f917fd3cdd49cbc225fc81302168fcf3 Mon Sep 17 00:00:00 2001 From: Marcin Serwin Date: Wed, 6 May 2020 17:29:58 +0200 Subject: [PATCH 8/9] Fix lint examples --- clippy_lints/src/no_to_string_in_display.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/no_to_string_in_display.rs b/clippy_lints/src/no_to_string_in_display.rs index cdd050b5480e..c3a1148e734b 100644 --- a/clippy_lints/src/no_to_string_in_display.rs +++ b/clippy_lints/src/no_to_string_in_display.rs @@ -1,6 +1,6 @@ use crate::utils::{match_def_path, paths, span_lint}; use if_chain::if_chain; -use rustc_hir::{Expr, ExprKind, FnDecl, FnSig, ImplItem, ImplItemKind, MutTy, Node, Path, QPath, Ty, TyKind, def}; +use rustc_hir::{def, Expr, ExprKind, FnDecl, FnSig, ImplItem, ImplItemKind, MutTy, Node, Path, QPath, Ty, TyKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -17,6 +17,9 @@ declare_clippy_lint! { /// **Example:** /// /// ```rust + /// use std::fmt; + /// + /// struct Structure(i32); /// impl fmt::Display for Structure { /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { /// write!(f, "{}", self.to_string()) @@ -26,6 +29,9 @@ declare_clippy_lint! { /// ``` /// Use instead: /// ```rust + /// use std::fmt; + /// + /// struct Structure(i32); /// impl fmt::Display for Structure { /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { /// write!(f, "{}", self.0) From 6d1d965bfb8a72b5287ccd644a32fa9938014550 Mon Sep 17 00:00:00 2001 From: Marcin Serwin Date: Wed, 6 May 2020 17:56:09 +0200 Subject: [PATCH 9/9] Fix lint name --- CHANGELOG.md | 2 +- clippy_lints/src/lib.rs | 10 +++++----- ...tring_in_display.rs => to_string_in_display.rs} | 8 ++++---- src/lintlist/mod.rs | 14 +++++++------- ...tring_in_display.rs => to_string_in_display.rs} | 2 +- ..._display.stderr => to_string_in_display.stderr} | 4 ++-- 6 files changed, 20 insertions(+), 20 deletions(-) rename clippy_lints/src/{no_to_string_in_display.rs => to_string_in_display.rs} (92%) rename tests/ui/{no_to_string_in_display.rs => to_string_in_display.rs} (91%) rename tests/ui/{no_to_string_in_display.stderr => to_string_in_display.stderr} (66%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c45c72afdef..84222a572e94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1487,7 +1487,6 @@ Released 2018-09-13 [`new_ret_no_self`]: https://rust-lang.github.io/rust-clippy/master/index.html#new_ret_no_self [`new_without_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default [`no_effect`]: https://rust-lang.github.io/rust-clippy/master/index.html#no_effect -[`no_to_string_in_display`]: https://rust-lang.github.io/rust-clippy/master/index.html#no_to_string_in_display [`non_ascii_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_ascii_literal [`nonminimal_bool`]: https://rust-lang.github.io/rust-clippy/master/index.html#nonminimal_bool [`nonsensical_open_options`]: https://rust-lang.github.io/rust-clippy/master/index.html#nonsensical_open_options @@ -1580,6 +1579,7 @@ Released 2018-09-13 [`temporary_assignment`]: https://rust-lang.github.io/rust-clippy/master/index.html#temporary_assignment [`temporary_cstring_as_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#temporary_cstring_as_ptr [`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 [`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 diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 95d9d9bb4e58..f22d2f7d065c 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -279,7 +279,6 @@ mod neg_cmp_op_on_partial_ord; mod neg_multiply; mod new_without_default; mod no_effect; -mod no_to_string_in_display; mod non_copy_const; mod non_expressive_names; mod open_options; @@ -311,6 +310,7 @@ mod swap; mod tabs_in_doc_comments; mod temporary_assignment; mod to_digit_is_some; +mod to_string_in_display; mod trait_bounds; mod transmute; mod transmuting_null; @@ -745,7 +745,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: &new_without_default::NEW_WITHOUT_DEFAULT, &no_effect::NO_EFFECT, &no_effect::UNNECESSARY_OPERATION, - &no_to_string_in_display::NO_TO_STRING_IN_DISPLAY, &non_copy_const::BORROW_INTERIOR_MUTABLE_CONST, &non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST, &non_expressive_names::JUST_UNDERSCORES_AND_DIGITS, @@ -799,6 +798,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: &tabs_in_doc_comments::TABS_IN_DOC_COMMENTS, &temporary_assignment::TEMPORARY_ASSIGNMENT, &to_digit_is_some::TO_DIGIT_IS_SOME, + &to_string_in_display::TO_STRING_IN_DISPLAY, &trait_bounds::TYPE_REPETITION_IN_BOUNDS, &transmute::CROSSPOINTER_TRANSMUTE, &transmute::TRANSMUTE_BYTES_TO_STR, @@ -1019,7 +1019,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_early_pass(|| box reference::DerefAddrOf); store.register_early_pass(|| box reference::RefInDeref); store.register_early_pass(|| box double_parens::DoubleParens); - store.register_late_pass(|| box no_to_string_in_display::NoToStringInDisplay); + store.register_late_pass(|| box to_string_in_display::ToStringInDisplay); store.register_early_pass(|| box unsafe_removed_from_name::UnsafeNameRemoval); store.register_early_pass(|| box if_not_else::IfNotElse); store.register_early_pass(|| box else_if_without_else::ElseIfWithoutElse); @@ -1365,7 +1365,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&new_without_default::NEW_WITHOUT_DEFAULT), LintId::of(&no_effect::NO_EFFECT), LintId::of(&no_effect::UNNECESSARY_OPERATION), - LintId::of(&no_to_string_in_display::NO_TO_STRING_IN_DISPLAY), LintId::of(&non_copy_const::BORROW_INTERIOR_MUTABLE_CONST), LintId::of(&non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST), LintId::of(&non_expressive_names::JUST_UNDERSCORES_AND_DIGITS), @@ -1406,6 +1405,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&tabs_in_doc_comments::TABS_IN_DOC_COMMENTS), LintId::of(&temporary_assignment::TEMPORARY_ASSIGNMENT), LintId::of(&to_digit_is_some::TO_DIGIT_IS_SOME), + LintId::of(&to_string_in_display::TO_STRING_IN_DISPLAY), LintId::of(&transmute::CROSSPOINTER_TRANSMUTE), LintId::of(&transmute::TRANSMUTE_BYTES_TO_STR), LintId::of(&transmute::TRANSMUTE_FLOAT_TO_INT), @@ -1668,7 +1668,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&misc::FLOAT_CMP), LintId::of(&misc::MODULO_ONE), LintId::of(&mut_key::MUTABLE_KEY_TYPE), - LintId::of(&no_to_string_in_display::NO_TO_STRING_IN_DISPLAY), LintId::of(&non_copy_const::BORROW_INTERIOR_MUTABLE_CONST), LintId::of(&non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST), LintId::of(&open_options::NONSENSICAL_OPEN_OPTIONS), @@ -1679,6 +1678,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL), LintId::of(&suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL), LintId::of(&swap::ALMOST_SWAPPED), + LintId::of(&to_string_in_display::TO_STRING_IN_DISPLAY), LintId::of(&transmute::UNSOUND_COLLECTION_TRANSMUTE), LintId::of(&transmute::WRONG_TRANSMUTE), LintId::of(&transmuting_null::TRANSMUTING_NULL), diff --git a/clippy_lints/src/no_to_string_in_display.rs b/clippy_lints/src/to_string_in_display.rs similarity index 92% rename from clippy_lints/src/no_to_string_in_display.rs rename to clippy_lints/src/to_string_in_display.rs index c3a1148e734b..65fc367f4e12 100644 --- a/clippy_lints/src/no_to_string_in_display.rs +++ b/clippy_lints/src/to_string_in_display.rs @@ -38,14 +38,14 @@ declare_clippy_lint! { /// } /// } /// ``` - pub NO_TO_STRING_IN_DISPLAY, + pub TO_STRING_IN_DISPLAY, correctness, "to_string method used while implementing Display trait" } -declare_lint_pass!(NoToStringInDisplay => [NO_TO_STRING_IN_DISPLAY]); +declare_lint_pass!(ToStringInDisplay => [TO_STRING_IN_DISPLAY]); -impl LateLintPass<'_, '_> for NoToStringInDisplay { +impl LateLintPass<'_, '_> for ToStringInDisplay { fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &Expr<'_>) { let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id); let parent_node = cx.tcx.hir().find(parent_id); @@ -67,7 +67,7 @@ impl LateLintPass<'_, '_> for NoToStringInDisplay { then { span_lint( cx, - NO_TO_STRING_IN_DISPLAY, + TO_STRING_IN_DISPLAY, expr.span, "Using to_string in fmt::Display implementation might lead to infinite recursion", ); diff --git a/src/lintlist/mod.rs b/src/lintlist/mod.rs index 20bdd710b6cf..9bdff07b0dcd 100644 --- a/src/lintlist/mod.rs +++ b/src/lintlist/mod.rs @@ -1529,13 +1529,6 @@ pub static ref ALL_LINTS: Vec = vec![ deprecation: None, module: "no_effect", }, - Lint { - name: "no_to_string_in_display", - group: "correctness", - desc: "to_string method used while implementing Display trait", - deprecation: None, - module: "no_to_string_in_display", - }, Lint { name: "non_ascii_literal", group: "pedantic", @@ -2145,6 +2138,13 @@ pub static ref ALL_LINTS: Vec = vec![ deprecation: None, module: "to_digit_is_some", }, + Lint { + name: "to_string_in_display", + group: "correctness", + desc: "to_string method used while implementing Display trait", + deprecation: None, + module: "to_string_in_display", + }, Lint { name: "todo", group: "restriction", diff --git a/tests/ui/no_to_string_in_display.rs b/tests/ui/to_string_in_display.rs similarity index 91% rename from tests/ui/no_to_string_in_display.rs rename to tests/ui/to_string_in_display.rs index 3dbe02e854a4..6f11c619a099 100644 --- a/tests/ui/no_to_string_in_display.rs +++ b/tests/ui/to_string_in_display.rs @@ -1,4 +1,4 @@ -#![warn(clippy::no_to_string_in_display)] +#![warn(clippy::to_string_in_display)] use std::fmt; diff --git a/tests/ui/no_to_string_in_display.stderr b/tests/ui/to_string_in_display.stderr similarity index 66% rename from tests/ui/no_to_string_in_display.stderr rename to tests/ui/to_string_in_display.stderr index a6c20b3a93ff..2a326b314987 100644 --- a/tests/ui/no_to_string_in_display.stderr +++ b/tests/ui/to_string_in_display.stderr @@ -1,10 +1,10 @@ error: Using to_string in fmt::Display implementation might lead to infinite recursion - --> $DIR/no_to_string_in_display.rs:24:25 + --> $DIR/to_string_in_display.rs:24:25 | LL | write!(f, "{}", self.to_string()) | ^^^^^^^^^^^^^^^^ | - = note: `-D clippy::no-to-string-in-display` implied by `-D warnings` + = note: `-D clippy::to-string-in-display` implied by `-D warnings` error: aborting due to previous error