rust/tests/ui/dbg_macro/dbg_macro.fixed
bors 806a9dfddd Auto merge of #13464 - y21:issue13458, r=flip1995
Don't warn on proc macro generated code in `needless_return`

Fixes #13458
Fixes #13457
Fixes #13467
Fixes #13479
Fixes #13481
Fixes #13526
Fixes #13486

The fix is unfortunately a little more convoluted than just simply adding a `is_from_proc_macro`. That check *does*  fix the issue, however it also introduces a bunch of false negatives in the tests, specifically when the returned expression is in a different syntax context, e.g. `return format!(..)`.

The proc macro check builds up a start and end pattern based on the HIR nodes and compares it to a snippet of the span, however that would currently fail for `return format!(..)` because we would have the patterns `("return", <something inside of the format macro>)`, which doesn't compare equal. So we now return an empty string pattern for when it's in a different syntax context.

"Hide whitespace" helps a bit for reviewing the proc macro detection change

changelog: none
2024-10-10 11:34:13 +02:00

112 lines
2.6 KiB
Rust

#![warn(clippy::dbg_macro)]
#![allow(clippy::unnecessary_operation, clippy::no_effect, clippy::unit_arg)]
fn foo(n: u32) -> u32 {
if let Some(n) = n.checked_sub(4) { n } else { n }
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
}
fn bar(_: ()) {}
fn factorial(n: u32) -> u32 {
if n <= 1 {
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
1
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
} else {
n * factorial(n - 1)
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
}
}
fn main() {
42;
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
foo(3) + factorial(4);
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
(1, 2, 3, 4, 5);
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
}
fn issue9914() {
macro_rules! foo {
($x:expr) => {
$x;
};
}
macro_rules! foo2 {
($x:expr) => {
$x;
};
}
macro_rules! expand_to_dbg {
() => {
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
};
}
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
#[allow(clippy::let_unit_value)]
let _ = ();
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
bar(());
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
foo!(());
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
foo2!(foo!(()));
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
expand_to_dbg!();
}
mod issue7274 {
trait Thing<'b> {
fn foo(&self);
}
macro_rules! define_thing {
($thing:ident, $body:expr) => {
impl<'a> Thing<'a> for $thing {
fn foo<'b>(&self) {
$body
}
}
};
}
struct MyThing;
define_thing!(MyThing, {
2;
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
});
}
#[test]
pub fn issue8481() {
1;
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
}
#[cfg(test)]
fn foo2() {
1;
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
}
#[cfg(test)]
mod mod1 {
fn func() {
1;
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
}
}
mod issue12131 {
fn dbg_in_print(s: &str) {
println!("dbg: {:?}", s);
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
print!("{}", s);
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
}
}