-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fix string_lit_as_bytes lint for macros #3217
Conversation
This is a WIP that doesn't work and I could use some help / mentoring on it. I moved the check in question to a new file because it seemed like failing an EarlyLintPass lint causes LateLintPass lints not to be run. Also I'm not sure if im going about this entirely the wrong way. I expected to be able to use |
For what its worth, the debug traces that I added, which are included in this initial commit, produced the following output
I was hoping the stringify or include_str would indicate that it was a macro :/ |
|
re: moving it back to strings.rs. At the very least I think I'd still have to split up the strings.rs test case because there are still other late pass lints in there. What ended up happening was I moved the lint from late to early and it changed the number of lints in the stderr output from 11 to 2. Does it make more sense to have one |
Multiple test files are always better. We want to keep the size of the |
K I think we're getting close, I'm getting a strange failure on run-pass/used_underscore_binding_macro.rs. Also should I try to add |
No, we need to add No
NIT: To make the life of the reviewer (me) easier, it would be great if you could add a new commit when you change something. It is hard to keep up with |
clippy_lints/src/strings.rs
Outdated
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &crate::syntax::ast::Expr) { | ||
use crate::syntax::ast::{ExprKind, LitKind}; | ||
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) { | ||
use self::ast::{ExprKind, LitKind}; |
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 does this need the self::
... path segment? is that what this is called? The type for e
in the function args doesn't need it.
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.
The type in the function call just looks for the use
statements of the file and can use them directly. use
-statements on the other hand also need to be able to import stuff from outside the current file, so you have to specify where it should start looking.
- Crate level:
crate::foo
in 2018 edition,::foo
in 2015 edition - Module level:
self::foo
- Parent-module level:
super::foo
(parent-parent-module:super::super::foo
, and so on)
If you just write use foo
in the 2018 edition (extern crate
got removed) it will look for a crate named foo
, whereas use self::foo
looks for a module/function/... inside the current module.
I found that a big problem with pre-expansion lints is that they won't lint code "passed through" macros. Example:
After this lint is changed to pre-expansion, it won't check inside the For fixing this bug, I found a possible alternative. In the original code, |
Oh yeah, you're right, I never thought about that. The code inside the macro call isn't even parsed at the point where the
@yaahallo Could you make this change? Sorry for the back and forth. |
@flip1995 of course :D |
f211037
to
4e4f091
Compare
Also, I'm sorry this took so long :/ I had it mostly ready and just never set aside the time to finish the last 5% and push it. |
No problem, we've all been there :D I'll try to take a closer look at this in the next two days. |
Now I'm sorry this took so long :/ I took last week off and couldn't look over this last weekend. Everything except the 4 comments I left LGTM (Needs a rebase) |
Prior to this change, string_lit_as_bytes would trigger for constructs like `include_str!("filename").as_bytes()` and would recommend fixing it by rewriting as `binclude_str!("filename")`. This change updates the lint to act as an EarlyLintPass lint. It then differentiates between string literals and macros that have bytes yielding alternatives. Closes rust-lang#3205
looks good to me, too! bors r+ |
3217: Fix string_lit_as_bytes lint for macros r=phansch a=yaahallo Prior to this change, string_lit_as_bytes would trigger for constructs like `include_str!("filename").as_bytes()` and would recommend fixing it by rewriting as `binclude_str!("filename")`. This change updates the lint to act as an EarlyLintPass lint. It then differentiates between string literals and macros that have bytes yielding alternatives. Closes #3205 3366: Don't expand macros in some suggestions r=oli-obk a=phansch Fixes #1148 Fixes #1628 Fixes #2455 Fixes #3023 Fixes #3333 Fixes #3360 Co-authored-by: Jane Lusby <jlusby42@gmail.com> Co-authored-by: Philipp Hansch <dev@phansch.net>
Prior to this change, string_lit_as_bytes would trigger for constructs
like
include_str!("filename").as_bytes()
and would recommend fixing itby rewriting as
binclude_str!("filename")
.This change updates the lint to act as an EarlyLintPass lint. It then
differentiates between string literals and macros that have bytes
yielding alternatives.
Closes #3205