2021-03-16 00:55:45 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
2021-04-12 23:01:32 +00:00
|
|
|
use clippy_utils::{is_diag_trait_item, match_def_path, path_to_local_id, paths};
|
2020-08-28 14:10:16 +00:00
|
|
|
use if_chain::if_chain;
|
2021-01-15 09:56:44 +00:00
|
|
|
use rustc_hir::{Expr, ExprKind, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind};
|
2020-08-28 14:10:16 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-02-27 01:10:17 +00:00
|
|
|
use rustc_span::symbol::sym;
|
2020-08-28 14:10:16 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for uses of `to_string()` in `Display` traits.
|
2020-08-28 14:10:16 +00:00
|
|
|
///
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Usually `to_string` is implemented indirectly
|
2020-08-28 14:10:16 +00:00
|
|
|
/// via `Display`. Hence using it while implementing `Display` would
|
|
|
|
/// lead to infinite recursion.
|
|
|
|
///
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### Example
|
2020-08-28 14:10:16 +00:00
|
|
|
///
|
|
|
|
/// ```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())
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// 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)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 19:06:26 +00:00
|
|
|
#[clippy::version = "1.48.0"]
|
2020-08-28 14:10:16 +00:00
|
|
|
pub TO_STRING_IN_DISPLAY,
|
|
|
|
correctness,
|
|
|
|
"`to_string` method used while implementing `Display` trait"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ToStringInDisplay {
|
|
|
|
in_display_impl: bool,
|
|
|
|
self_hir_id: Option<HirId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToStringInDisplay {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
in_display_impl: false,
|
|
|
|
self_hir_id: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(ToStringInDisplay => [TO_STRING_IN_DISPLAY]);
|
|
|
|
|
|
|
|
impl LateLintPass<'_> for ToStringInDisplay {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
|
|
|
|
if is_display_impl(cx, item) {
|
|
|
|
self.in_display_impl = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item_post(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
|
|
|
|
if is_display_impl(cx, item) {
|
|
|
|
self.in_display_impl = false;
|
|
|
|
self.self_hir_id = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
|
|
|
|
if_chain! {
|
|
|
|
if self.in_display_impl;
|
|
|
|
if let ImplItemKind::Fn(.., body_id) = &impl_item.kind;
|
|
|
|
let body = cx.tcx.hir().body(*body_id);
|
|
|
|
if !body.params.is_empty();
|
|
|
|
then {
|
|
|
|
let self_param = &body.params[0];
|
|
|
|
self.self_hir_id = Some(self_param.pat.hir_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
|
|
|
if_chain! {
|
2021-02-02 01:22:31 +00:00
|
|
|
if self.in_display_impl;
|
|
|
|
if let Some(self_hir_id) = self.self_hir_id;
|
2021-09-04 16:21:49 +00:00
|
|
|
if let ExprKind::MethodCall(path, _, [ref self_arg, ..], _) = expr.kind;
|
2020-08-28 14:10:16 +00:00
|
|
|
if path.ident.name == sym!(to_string);
|
2021-02-27 01:10:17 +00:00
|
|
|
if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
|
2021-04-12 23:01:32 +00:00
|
|
|
if is_diag_trait_item(cx, expr_def_id, sym::ToString);
|
2021-09-04 16:21:49 +00:00
|
|
|
if path_to_local_id(self_arg, self_hir_id);
|
2020-08-28 14:10:16 +00:00
|
|
|
then {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
TO_STRING_IN_DISPLAY,
|
|
|
|
expr.span,
|
|
|
|
"using `to_string` in `fmt::Display` implementation might lead to infinite recursion",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_display_impl(cx: &LateContext<'_>, item: &Item<'_>) -> bool {
|
|
|
|
if_chain! {
|
2020-11-22 22:46:21 +00:00
|
|
|
if let ItemKind::Impl(Impl { of_trait: Some(trait_ref), .. }) = &item.kind;
|
2020-08-28 14:10:16 +00:00
|
|
|
if let Some(did) = trait_ref.trait_def_id();
|
|
|
|
then {
|
|
|
|
match_def_path(cx, did, &paths::DISPLAY_TRAIT)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|