-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add PRINTLN_EMPTY_STRING lint. #2146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
clippy_lints/src/print.rs
Outdated
"print" => check_print(cx, span, args), | ||
"println" => check_println(cx, span, args), | ||
_ => (), | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was getting a cyclomatic_complexity
lint, so I refactored this part.
fac1419
to
88d9ec9
Compare
clippy_lints/src/print.rs
Outdated
let ExprPath(ref qpath) = args_fun.node, | ||
let Some(const_def_id) = opt_def_id(resolve_node(cx, qpath, args_fun.hir_id)), | ||
match_def_path(cx.tcx, const_def_id, &paths::FMT_ARGUMENTS_NEWV1), | ||
args_args.len() == 2, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you deduplicate the above lines? check_print
has the same lines.
clippy_lints/src/print.rs
Outdated
// HACK: check size of the macro invocation: | ||
// println!(); // 11 chars: don't lint | ||
// println!("") // 12 chars: lint | ||
(span.hi() - span.lo()).0 > 11, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hahahaha! That is awesome, but I think it would be better if you checked whether the code snippet matches println!("")
tests/ui/println_empty_string.rs
Outdated
fn main() { | ||
|
||
println!(); | ||
println!("") // no semicolon intended |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why no semicolon?
clippy_lints/src/print.rs
Outdated
(span.hi() - span.lo()).0 > 11, | ||
// check the presence of that string | ||
let Ok(snippet) = cx.sess().codemap().span_to_snippet(span), | ||
snippet.contains("\"\""), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually meant that you could just do snippet == "println!(\"\")"
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tested that this snippet can contain whitespace or a semicolon, and your method doesn't detect them. Is there a better way I can test the presence of an empty argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know of any, but you're right, this should work for all realistic cases
Thanks for the review @oli-obk! |
4e0d932
to
22f3ca0
Compare
Thanks, this looks good to me now. |
Since the merge of rust-lang#2146, `Cargo.lock` is no longer checked in the git repository, but `cargo` generates it anyway, if we are not in the main rust repository. Ignore it to avoid eventual confusion when contributing directly to clippy.
I have no idea of how I could properly check that there is really an empty string argument in a
println
invocation.I think this will need the pre-expanded AST, which I can't access even in an
EarlyLintPass
.See the
HACK
inprint.rs
for details.Will fix #2135 when ready.