Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions clippy_lints/src/write/empty_string.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::macros::MacroCall;
use clippy_utils::source::expand_past_previous_comma;
use clippy_utils::source::{SpanRangeExt, expand_past_previous_comma};
use clippy_utils::sym;
use rustc_ast::{FormatArgs, FormatArgsPiece};
use rustc_errors::Applicability;
use rustc_lint::LateContext;
use rustc_span::BytePos;

use super::{PRINTLN_EMPTY_STRING, WRITELN_EMPTY_STRING};

pub(super) fn check(cx: &LateContext<'_>, format_args: &FormatArgs, macro_call: &MacroCall, name: &str) {
if let [FormatArgsPiece::Literal(sym::LF)] = &format_args.template[..] {
let mut span = format_args.span;

// Check if the next character is a comma after empty string literal
if let Some(forward_span) = Some(span.with_hi(span.hi() + BytePos(1)))
&& forward_span.check_source_text(cx, |s| s.ends_with(','))
{
span = forward_span;
}
let lint = if name == "writeln" {
span = expand_past_previous_comma(cx, span);

Expand Down
38 changes: 38 additions & 0 deletions tests/ui/println_empty_string.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,41 @@ fn main() {
//~^ println_empty_string
}
}

fn issue_16167() {
println!();
//~^ println_empty_string
match "a" {
_ => println!(),
//~^ println_empty_string
}

eprintln!();
//~^ println_empty_string
match "a" {
_ => eprintln!(),
//~^ println_empty_string
}

//~v println_empty_string
println!(

);
match "a" {
//~v println_empty_string
_ => println!(

),
}

//~v println_empty_string
eprintln!(

);
match "a" {
//~v println_empty_string
_ => eprintln!(

),
}
}
46 changes: 46 additions & 0 deletions tests/ui/println_empty_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,49 @@ fn main() {
//~^ println_empty_string
}
}

fn issue_16167() {
println!("",);
//~^ println_empty_string
match "a" {
_ => println!("",),
//~^ println_empty_string
}

eprintln!("",);
//~^ println_empty_string
match "a" {
_ => eprintln!("",),
//~^ println_empty_string
}

//~v println_empty_string
println!(
"\
\
",
);
match "a" {
//~v println_empty_string
_ => println!(
"\
\
",
),
}

//~v println_empty_string
eprintln!(
"\
\
",
);
match "a" {
//~v println_empty_string
_ => eprintln!(
"\
\
",
),
}
}
80 changes: 79 additions & 1 deletion tests/ui/println_empty_string.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,83 @@ LL | _ => eprintln!(""),
| |
| help: remove the empty string

error: aborting due to 4 previous errors
error: empty string literal in `println!`
--> tests/ui/println_empty_string.rs:24:5
|
LL | println!("",);
| ^^^^^^^^^---^
| |
| help: remove the empty string

error: empty string literal in `println!`
--> tests/ui/println_empty_string.rs:27:14
|
LL | _ => println!("",),
| ^^^^^^^^^---^
| |
| help: remove the empty string

error: empty string literal in `eprintln!`
--> tests/ui/println_empty_string.rs:31:5
|
LL | eprintln!("",);
| ^^^^^^^^^^---^
| |
| help: remove the empty string

error: empty string literal in `eprintln!`
--> tests/ui/println_empty_string.rs:34:14
|
LL | _ => eprintln!("",),
| ^^^^^^^^^^---^
| |
| help: remove the empty string

error: empty string literal in `println!`
--> tests/ui/println_empty_string.rs:39:5
|
LL | / println!(
LL | |/ "\
LL | || \
LL | || ",
| ||__________________- help: remove the empty string
LL | | );
| |______^

error: empty string literal in `println!`
--> tests/ui/println_empty_string.rs:46:14
|
LL | _ => println!(
| _______________^
LL | |/ "\
LL | || \
LL | || ",
| ||______________________- help: remove the empty string
LL | | ),
| |__________^

error: empty string literal in `eprintln!`
--> tests/ui/println_empty_string.rs:54:5
|
LL | / eprintln!(
LL | |/ "\
LL | || \
LL | || ",
| ||__________________- help: remove the empty string
LL | | );
| |______^

error: empty string literal in `eprintln!`
--> tests/ui/println_empty_string.rs:61:14
|
LL | _ => eprintln!(
| _______________^
LL | |/ "\
LL | || \
LL | || ",
| ||______________________- help: remove the empty string
LL | | ),
| |__________^

error: aborting due to 12 previous errors

Loading