Merge pull request #2650 from thekidxp/fixEmptyPrintln

fixEmptyPrintln
This commit is contained in:
Oliver Schneider 2018-04-15 14:30:45 +02:00 committed by GitHub
commit 486dc5c070
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 12 deletions

3
.gitignore vendored
View file

@ -28,5 +28,6 @@ util/gh-pages/lints.json
*.rs.bk
helper.txt
*.iml
.vscode
.idea

View file

@ -12,7 +12,7 @@ use url::Url;
///
/// **Why is this bad?** *Rustdoc* supports markdown formatting, `_`, `::` and
/// camel-case probably indicates some code which should be included between
/// ticks. `_` can also be used for empasis in markdown, this lint tries to
/// ticks. `_` can also be used for emphasis in markdown, this lint tries to
/// consider that.
///
/// **Known problems:** Lots of bad docs wont be fixed, what the lint checks

View file

@ -230,11 +230,11 @@ fn check_write_variants<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr,
"using `write!()` with a format string that ends in a \
newline, consider using `writeln!()` instead");
},
"writeln" => if has_empty_arg(cx, span, fmtstr, fmtlen) {
"writeln" => if let Some(final_span) = has_empty_arg(cx, span, fmtstr, fmtlen) {
span_lint_and_sugg(
cx,
WRITE_WITH_NEWLINE,
span,
final_span,
"using `writeln!(v, \"\")`",
"replace it with",
"writeln!(v)".to_string(),
@ -295,11 +295,11 @@ fn check_print_variants<'a, 'tcx>(
newline, consider using `println!()` instead");
},
"println" =>
if has_empty_arg(cx, span, fmtstr, fmtlen) {
if let Some(final_span) = has_empty_arg(cx, span, fmtstr, fmtlen) {
span_lint_and_sugg(
cx,
PRINT_WITH_NEWLINE,
span,
final_span,
"using `println!(\"\")`",
"replace it with",
"println!()".to_string(),
@ -390,7 +390,7 @@ fn has_newline_end(args: &HirVec<Expr>, fmtstr: InternedString, fmtlen: usize) -
}
/// Check for writeln!(v, "") / println!("")
fn has_empty_arg<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, span: Span, fmtstr: InternedString, fmtlen: usize) -> bool {
fn has_empty_arg<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, span: Span, fmtstr: InternedString, fmtlen: usize) -> Option<Span> {
if_chain! {
// check that the string is empty
if fmtlen == 1;
@ -400,10 +400,13 @@ fn has_empty_arg<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, span: Span, fmtstr: Inter
if let Ok(snippet) = cx.sess().codemap().span_to_snippet(span);
if snippet.contains("\"\"");
then {
return true
if snippet.ends_with(';') {
return Some(cx.sess().codemap().span_until_char(span, ';'));
}
return Some(span)
}
}
false
None
}
/// Returns the slice of format string parts in an `Arguments::new_v1` call.

View file

@ -1,4 +1,8 @@
fn main() {
println!();
println!("");
match "a" {
_ => println!(""),
}
}

View file

@ -2,9 +2,15 @@ error: using `println!("")`
--> $DIR/println_empty_string.rs:3:5
|
3 | println!("");
| ^^^^^^^^^^^^^ help: replace it with: `println!()`
| ^^^^^^^^^^^^ help: replace it with: `println!()`
|
= note: `-D print-with-newline` implied by `-D warnings`
error: aborting due to previous error
error: using `println!("")`
--> $DIR/println_empty_string.rs:6:14
|
6 | _ => println!(""),
| ^^^^^^^^^^^^ help: replace it with: `println!()`
error: aborting due to 2 previous errors

View file

@ -2,7 +2,7 @@ error: using `writeln!(v, "")`
--> $DIR/writeln_empty_string.rs:9:5
|
9 | writeln!(&mut v, "");
| ^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `writeln!(v)`
| ^^^^^^^^^^^^^^^^^^^^ help: replace it with: `writeln!(v)`
|
= note: `-D write-with-newline` implied by `-D warnings`