Skip to content

Commit

Permalink
Use {:?} spans in use_debug
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexendoo committed Mar 30, 2022
1 parent a95d135 commit a56a0d9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 32 deletions.
40 changes: 14 additions & 26 deletions clippy_lints/src/write.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::macros::{root_macro_call_first_node, FormatArgsExpn, MacroCall};
use clippy_utils::macros::{root_macro_call_first_node, FormatArgsArg, FormatArgsExpn, MacroCall};
use clippy_utils::source::snippet_opt;
use rustc_ast::LitKind;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -275,6 +275,9 @@ impl<'tcx> LateLintPass<'tcx> for Write {
}

let Some(format_args) = FormatArgsExpn::find_nested(cx, expr, macro_call.expn) else { return };
if format_args.format_string_span.from_expansion() {
return;
}

match diag_name {
sym::print_macro | sym::eprint_macro | sym::write_macro => {
Expand All @@ -286,20 +289,15 @@ impl<'tcx> LateLintPass<'tcx> for Write {
_ => {},
}

check_literal(cx, &format_args, name);
let Some(args) = format_args.args(cx) else { return };
check_literal(cx, &args, name, format_args.is_raw(cx));

if !self.in_debug_impl
&& format_args
.formatters
.iter()
.any(|&(_, formatter)| formatter == sym::Debug)
{
span_lint(
cx,
USE_DEBUG,
format_args.format_string_span,
"use of `Debug`-based formatting",
);
if !self.in_debug_impl {
for arg in args {
if arg.format_trait == sym::Debug {
span_lint(cx, USE_DEBUG, arg.span, "use of `Debug`-based formatting");
}
}
}
}
}
Expand Down Expand Up @@ -335,7 +333,6 @@ fn check_newline(cx: &LateContext<'_>, format_args: &FormatArgsExpn<'_>, macro_c
};

if_chain! {
if !format_string_span.from_expansion();
if last.as_str().ends_with('\n');

// ignore format strings with other internal vertical whitespace
Expand Down Expand Up @@ -401,9 +398,6 @@ fn check_empty_string(cx: &LateContext<'_>, format_args: &FormatArgsExpn<'_>, ma
let mut span = format_args.format_string_span;

if part.as_str() == "\n";
// `println!()` is also represented with an "\n" format string part, but
// its span is from an expansion
if !span.from_expansion();
then {
let lint = if name == "writeln" {
span = expand_past_previous_comma(cx, span);
Expand Down Expand Up @@ -432,15 +426,9 @@ fn check_empty_string(cx: &LateContext<'_>, format_args: &FormatArgsExpn<'_>, ma
}
}

fn check_literal<'tcx>(cx: &LateContext<'tcx>, format_args: &FormatArgsExpn<'tcx>, name: &str) {
if format_args.format_string_span.from_expansion() {
return;
}
let raw = format_args.is_raw(cx);

let Some(args) = format_args.args(cx) else { return };
fn check_literal(cx: &LateContext<'_>, args: &[FormatArgsArg<'_>], name: &str, raw: bool) {
let mut counts = HirIdMap::<usize>::default();
for arg in &args {
for arg in args {
*counts.entry(arg.value.hir_id).or_default() += 1;
}

Expand Down
12 changes: 6 additions & 6 deletions tests/ui/print.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: use of `Debug`-based formatting
--> $DIR/print.rs:11:19
--> $DIR/print.rs:11:20
|
LL | write!(f, "{:?}", 43.1415)
| ^^^^^^
| ^^^^
|
= note: `-D clippy::use-debug` implied by `-D warnings`

Expand Down Expand Up @@ -33,10 +33,10 @@ LL | print!("Hello {:?}", "World");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: use of `Debug`-based formatting
--> $DIR/print.rs:28:12
--> $DIR/print.rs:28:19
|
LL | print!("Hello {:?}", "World");
| ^^^^^^^^^^^^
| ^^^^

error: use of `print!`
--> $DIR/print.rs:30:5
Expand All @@ -45,10 +45,10 @@ LL | print!("Hello {:#?}", "#orld");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: use of `Debug`-based formatting
--> $DIR/print.rs:30:12
--> $DIR/print.rs:30:19
|
LL | print!("Hello {:#?}", "#orld");
| ^^^^^^^^^^^^^
| ^^^^^

error: aborting due to 8 previous errors

0 comments on commit a56a0d9

Please sign in to comment.