Skip to content

Commit 22f3ca0

Browse files
committed
Add PRINTLN_EMPTY_STRING lint.
1 parent 4d9ed8b commit 22f3ca0

File tree

4 files changed

+78
-16
lines changed

4 files changed

+78
-16
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
530530
partialeq_ne_impl::PARTIALEQ_NE_IMPL,
531531
precedence::PRECEDENCE,
532532
print::PRINT_WITH_NEWLINE,
533+
print::PRINTLN_EMPTY_STRING,
533534
ptr::CMP_NULL,
534535
ptr::MUT_FROM_REF,
535536
ptr::PTR_ARG,

clippy_lints/src/print.rs

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
1+
use std::ops::Deref;
12
use rustc::hir::*;
23
use rustc::hir::map::Node::{NodeImplItem, NodeItem};
34
use rustc::lint::*;
45
use syntax::ast::LitKind;
56
use syntax::symbol::InternedString;
7+
use syntax_pos::Span;
68
use utils::{is_expn_of, match_def_path, match_path, resolve_node, span_lint};
79
use utils::{paths, opt_def_id};
810

11+
/// **What it does:** This lint warns when you using `println!("")` to
12+
/// print a newline.
13+
///
14+
/// **Why is this bad?** You should use `println!()`, which is simpler.
15+
///
16+
/// **Known problems:** None.
17+
///
18+
/// **Example:**
19+
/// ```rust
20+
/// println!("");
21+
/// ```
22+
declare_lint! {
23+
pub PRINTLN_EMPTY_STRING,
24+
Warn,
25+
"using `print!()` with a format string that ends in a newline"
26+
}
27+
928
/// **What it does:** This lint warns when you using `print!()` with a format
1029
/// string that
1130
/// ends in a newline.
@@ -64,7 +83,7 @@ pub struct Pass;
6483

6584
impl LintPass for Pass {
6685
fn get_lints(&self) -> LintArray {
67-
lint_array!(PRINT_WITH_NEWLINE, PRINT_STDOUT, USE_DEBUG)
86+
lint_array!(PRINT_WITH_NEWLINE, PRINTLN_EMPTY_STRING, PRINT_STDOUT, USE_DEBUG)
6887
}
6988
}
7089

@@ -88,10 +107,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
88107

89108
span_lint(cx, PRINT_STDOUT, span, &format!("use of `{}!`", name));
90109

91-
// Check print! with format string ending in "\n".
92110
if_let_chain!{[
93-
name == "print",
94-
95111
// ensure we're calling Arguments::new_v1
96112
args.len() == 1,
97113
let ExprCall(ref args_fun, ref args_args) = args[0].node,
@@ -102,20 +118,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
102118
let ExprAddrOf(_, ref match_expr) = args_args[1].node,
103119
let ExprMatch(ref args, _, _) = match_expr.node,
104120
let ExprTup(ref args) = args.node,
105-
106-
// collect the format string parts and check the last one
107121
let Some((fmtstr, fmtlen)) = get_argument_fmtstr_parts(&args_args[0]),
108-
let Some('\n') = fmtstr.chars().last(),
109-
110-
// "foo{}bar" is made into two strings + one argument,
111-
// if the format string starts with `{}` (eg. "{}foo"),
112-
// the string array is prepended an empty string "".
113-
// We only want to check the last string after any `{}`:
114-
args.len() < fmtlen,
115122
], {
116-
span_lint(cx, PRINT_WITH_NEWLINE, span,
117-
"using `print!()` with a format string that ends in a \
118-
newline, consider using `println!()` instead");
123+
match name {
124+
"print" => check_print(cx, span, args, fmtstr, fmtlen),
125+
"println" => check_println(cx, span, fmtstr, fmtlen),
126+
_ => (),
127+
}
119128
}}
120129
}
121130
}
@@ -135,6 +144,46 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
135144
}
136145
}
137146

147+
// Check for print!("... \n", ...).
148+
fn check_print<'a, 'tcx>(
149+
cx: &LateContext<'a, 'tcx>,
150+
span: Span,
151+
args: &HirVec<Expr>,
152+
fmtstr: InternedString,
153+
fmtlen: usize,
154+
) {
155+
if_let_chain!{[
156+
// check the final format string part
157+
let Some('\n') = fmtstr.chars().last(),
158+
159+
// "foo{}bar" is made into two strings + one argument,
160+
// if the format string starts with `{}` (eg. "{}foo"),
161+
// the string array is prepended an empty string "".
162+
// We only want to check the last string after any `{}`:
163+
args.len() < fmtlen,
164+
], {
165+
span_lint(cx, PRINT_WITH_NEWLINE, span,
166+
"using `print!()` with a format string that ends in a \
167+
newline, consider using `println!()` instead");
168+
}}
169+
}
170+
171+
/// Check for println!("")
172+
fn check_println<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, span: Span, fmtstr: InternedString, fmtlen: usize) {
173+
if_let_chain!{[
174+
// check that the string is empty
175+
fmtlen == 1,
176+
fmtstr.deref() == "\n",
177+
178+
// check the presence of that string
179+
let Ok(snippet) = cx.sess().codemap().span_to_snippet(span),
180+
snippet.contains("\"\""),
181+
], {
182+
span_lint(cx, PRINT_WITH_NEWLINE, span,
183+
"using `println!(\"\")`, consider using `println!()` instead");
184+
}}
185+
}
186+
138187
fn is_in_debug_impl(cx: &LateContext, expr: &Expr) -> bool {
139188
let map = &cx.tcx.hir;
140189

tests/ui/println_empty_string.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
println!();
3+
println!("");
4+
}

tests/ui/println_empty_string.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: using `println!("")`, consider using `println!()` instead
2+
--> $DIR/println_empty_string.rs:3:5
3+
|
4+
3 | println!("");
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: `-D print-with-newline` implied by `-D warnings`
8+

0 commit comments

Comments
 (0)