Skip to content

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

Merged
merged 1 commit into from
Oct 20, 2017

Conversation

MaloJaffre
Copy link
Contributor

@MaloJaffre MaloJaffre commented Oct 17, 2017

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 in print.rs for details.

Will fix #2135 when ready.

"print" => check_print(cx, span, args),
"println" => check_println(cx, span, args),
_ => (),
}
Copy link
Contributor Author

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.

@MaloJaffre MaloJaffre force-pushed the println_empty_string branch 2 times, most recently from fac1419 to 88d9ec9 Compare October 17, 2017 17:08
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,
Copy link
Contributor

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.

// HACK: check size of the macro invocation:
// println!(); // 11 chars: don't lint
// println!("") // 12 chars: lint
(span.hi() - span.lo()).0 > 11,
Copy link
Contributor

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!("")

fn main() {

println!();
println!("") // no semicolon intended
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why no semicolon?

(span.hi() - span.lo()).0 > 11,
// check the presence of that string
let Ok(snippet) = cx.sess().codemap().span_to_snippet(span),
snippet.contains("\"\""),
Copy link
Contributor

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!(\"\")".

Copy link
Contributor Author

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?

Copy link
Contributor

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

@MaloJaffre
Copy link
Contributor Author

MaloJaffre commented Oct 20, 2017

Thanks for the review @oli-obk!
I refactored code, used a less hacky way to check the presence of an empty string, and squashed my commits.
This PR is now ready to be merged, if there is no additional issues.

@MaloJaffre MaloJaffre changed the title [WIP] Add PRINTLN_EMPTY_STRING lint. Add PRINTLN_EMPTY_STRING lint. Oct 20, 2017
@MaloJaffre MaloJaffre force-pushed the println_empty_string branch from 4e0d932 to 22f3ca0 Compare October 20, 2017 14:46
@oli-obk oli-obk merged commit 0e489f3 into rust-lang:master Oct 20, 2017
@oli-obk
Copy link
Contributor

oli-obk commented Oct 20, 2017

Thanks, this looks good to me now.

@MaloJaffre MaloJaffre deleted the println_empty_string branch October 20, 2017 16:24
MaloJaffre added a commit to MaloJaffre/rust-clippy that referenced this pull request Oct 20, 2017
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggest println!() instead of println!("")
2 participants