-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
226 additions
and
0 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
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,118 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::macros::root_macro_call_first_node; | ||
use clippy_utils::{get_parent_as_impl, match_any_diagnostic_items}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, Impl, ImplItem, ImplItemKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_tool_lint, impl_lint_pass}; | ||
use rustc_span::{sym, Symbol}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for use of `println`, `print`, `eprintln` or `eprint` in an | ||
/// implementation of a formatting trait. | ||
/// | ||
/// ### Why is this bad? | ||
/// Printing during a formatting trait impl is likely unintentional. It can | ||
/// cause output to be split between the wrong streams. If `format!` is used | ||
/// text would go to stdout/stderr even if not desired. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// use std::fmt::{Display, Error, Formatter}; | ||
/// | ||
/// struct S; | ||
/// impl Display for S { | ||
/// fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { | ||
/// println!("S"); | ||
/// | ||
/// Ok(()) | ||
/// } | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// use std::fmt::{Display, Error, Formatter}; | ||
/// | ||
/// struct S; | ||
/// impl Display for S { | ||
/// fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { | ||
/// writeln!(f, "S"); | ||
/// | ||
/// Ok(()) | ||
/// } | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.59.0"] | ||
pub PRINT_IN_FMT, | ||
suspicious, | ||
"use of a print macro in a formatting trait impl" | ||
} | ||
|
||
struct FmtImpl { | ||
trait_name: Symbol, | ||
formatter_name: Option<Symbol>, | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct PrintInFmt { | ||
fmt_impl: Option<FmtImpl>, | ||
} | ||
|
||
impl_lint_pass!(PrintInFmt => [PRINT_IN_FMT]); | ||
|
||
impl LateLintPass<'_> for PrintInFmt { | ||
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &ImplItem<'_>) { | ||
let formatting_traits = [sym::Display, sym::Debug]; | ||
|
||
if_chain! { | ||
if impl_item.ident.name == sym::fmt; | ||
if let ImplItemKind::Fn(_, body_id) = impl_item.kind; | ||
if let Some(Impl { of_trait: Some(trait_ref),..}) = get_parent_as_impl(cx.tcx, impl_item.hir_id()); | ||
if let Some(trait_def_id) = trait_ref.trait_def_id(); | ||
if let Some(index) = match_any_diagnostic_items(cx, trait_def_id, &formatting_traits); | ||
then { | ||
let body = cx.tcx.hir().body(body_id); | ||
let formatter_name = body.params.get(1) | ||
.and_then(|param| param.pat.simple_ident()) | ||
.map(|ident| ident.name); | ||
|
||
self.fmt_impl = Some(FmtImpl { | ||
formatter_name, | ||
trait_name: formatting_traits[index], | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
fn check_impl_item_post(&mut self, _: &LateContext<'_>, _: &ImplItem<'_>) { | ||
self.fmt_impl = None; | ||
} | ||
|
||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if let Some(fmt_impl) = &self.fmt_impl { | ||
if let Some(macro_call) = root_macro_call_first_node(cx, expr) { | ||
let name = cx.tcx.item_name(macro_call.def_id); | ||
let replacement = match name.as_str() { | ||
"print" | "eprint" => "write", | ||
"println" | "eprintln" => "writeln", | ||
_ => return, | ||
}; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
PRINT_IN_FMT, | ||
macro_call.span, | ||
&format!("use of `{}!` in `{}` impl", name.as_str(), fmt_impl.trait_name), | ||
"replace with", | ||
if let Some(formatter_name) = fmt_impl.formatter_name { | ||
format!("{}!({}, ..)", replacement, formatter_name) | ||
} else { | ||
format!("{}!(..)", replacement) | ||
}, | ||
Applicability::HasPlaceholders, | ||
); | ||
} | ||
} | ||
} | ||
} |
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,56 @@ | ||
#![allow(unused, clippy::print_literal, clippy::write_literal)] | ||
#![warn(clippy::print_in_fmt)] | ||
use std::fmt::{Debug, Display, Error, Formatter}; | ||
|
||
macro_rules! indirect { | ||
() => {{ println!() }}; | ||
} | ||
|
||
macro_rules! nested { | ||
($($tt:tt)*) => { | ||
$($tt)* | ||
}; | ||
} | ||
|
||
struct Foo; | ||
impl Debug for Foo { | ||
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { | ||
print!("{}", 1); | ||
println!("{}", 2); | ||
eprint!("{}", 3); | ||
eprintln!("{}", 4); | ||
nested! { | ||
println!("nested"); | ||
}; | ||
|
||
write!(f, "{}", 5); | ||
writeln!(f, "{}", 6); | ||
indirect!(); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl Display for Foo { | ||
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { | ||
print!("Display"); | ||
write!(f, "Display"); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
struct UnnamedFormatter; | ||
impl Debug for UnnamedFormatter { | ||
fn fmt(&self, _: &mut Formatter) -> Result<(), Error> { | ||
println!("UnnamedFormatter"); | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn main() { | ||
print!("outside fmt"); | ||
println!("outside fmt"); | ||
eprint!("outside fmt"); | ||
eprintln!("outside fmt"); | ||
} |
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,46 @@ | ||
error: use of `print!` in `Debug` impl | ||
--> $DIR/print_in_fmt.rs:18:9 | ||
| | ||
LL | print!("{}", 1); | ||
| ^^^^^^^^^^^^^^^ help: replace with: `write!(f, ..)` | ||
| | ||
= note: `-D clippy::print-in-fmt` implied by `-D warnings` | ||
|
||
error: use of `println!` in `Debug` impl | ||
--> $DIR/print_in_fmt.rs:19:9 | ||
| | ||
LL | println!("{}", 2); | ||
| ^^^^^^^^^^^^^^^^^ help: replace with: `writeln!(f, ..)` | ||
|
||
error: use of `eprint!` in `Debug` impl | ||
--> $DIR/print_in_fmt.rs:20:9 | ||
| | ||
LL | eprint!("{}", 3); | ||
| ^^^^^^^^^^^^^^^^ help: replace with: `write!(f, ..)` | ||
|
||
error: use of `eprintln!` in `Debug` impl | ||
--> $DIR/print_in_fmt.rs:21:9 | ||
| | ||
LL | eprintln!("{}", 4); | ||
| ^^^^^^^^^^^^^^^^^^ help: replace with: `writeln!(f, ..)` | ||
|
||
error: use of `println!` in `Debug` impl | ||
--> $DIR/print_in_fmt.rs:23:13 | ||
| | ||
LL | println!("nested"); | ||
| ^^^^^^^^^^^^^^^^^^ help: replace with: `writeln!(f, ..)` | ||
|
||
error: use of `print!` in `Display` impl | ||
--> $DIR/print_in_fmt.rs:36:9 | ||
| | ||
LL | print!("Display"); | ||
| ^^^^^^^^^^^^^^^^^ help: replace with: `write!(f, ..)` | ||
|
||
error: use of `println!` in `Debug` impl | ||
--> $DIR/print_in_fmt.rs:46:9 | ||
| | ||
LL | println!("UnnamedFormatter"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `writeln!(..)` | ||
|
||
error: aborting due to 7 previous errors | ||
|