-
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
Macro use sugg #5279
Macro use sugg #5279
Conversation
30d9aff
to
2b4fbfc
Compare
clippy_lints/src/macro_use.rs
Outdated
for kid in lcx.tcx.item_children(id).iter() { | ||
if let Res::Def(DefKind::Macro(_mac_type), mac_id) = kid.res { | ||
let span = mac_attr.span.clone(); | ||
println!("{:#?}", lcx.tcx.def_path_str(mac_id)); | ||
self.imports.push((lcx.tcx.def_path_str(mac_id), span)); | ||
} | ||
} |
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.
This seems to work exactly how we need it to, so far it ignores all the modules the macro they are defined in but not found in. I need to figure out how to import from another crate into my test crate so I can have the macro stick in a module.
Note: since the prelude import is shadowed by the (builtin?) macro prelude import it doesn't catch that any more.
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.
For macros from another crate you can use the mini-macro
subcrate in the Clippy repo: https://github.com/rust-lang/rust-clippy/tree/master/mini-macro
This is also used in some other tests and includes a few proc-macros.
Since I'm not really sure, how far you're along with implementing this, can you give me a short summary, what you already implemented, what you think needs to be added, and which questions you still might have, before you can continue? |
It catches anything from any level of an imported crate directly imported // crate importing from "macros"
extern crate macro_rules;
#[macro_export]
macro_rules! pub_macro {
() => {
()
};
}
pub mod inner {
// RE-EXPORT
// this will stick in `inner` module
pub use macro_rules::try_err;
#[macro_export]
macro_rules! inner_mod {
() => {
#[allow(dead_code)]
pub struct Tardis;
};
}
}
// using in this crate
#[macro_use]
use mac;
fn main() {
pub_macro!();
inner_mod!();
// without the inner:: it fails to compile?
inner::try_err!();
} it correctly ignores modules only using the root/crate name, it also aliases correctly so it will use the renamed name. The re-export works (it will produce a suggestion of By iterating over the children of an imported I can update the .stderr file that may make it easier to see what I've done? |
☔ The latest upstream changes (presumably #5300) made this pull request unmergeable. Please resolve the merge conflicts. |
530fc20
to
a46ff1f
Compare
Since we know if we encountered a macro that wasn't found in the imports search, and therefor is one of the weird import kinds (std::prelude). Could we defer all the how they are gatheredif lcx.sess().opts.edition == Edition::Edition2018;
if let hir::ItemKind::Use(path, _kind) = &item.kind;
if let Some(mac_attr) = item
.attrs
.iter()
.find(|attr| attr.ident().map(|s| s.to_string()) == Some("macro_use".to_string()));
if let Res::Def(DefKind::Mod, id) = path.res;
then {
for kid in lcx.tcx.item_children(id).iter() {
if let Res::Def(DefKind::Macro(_mac_type), mac_id) = kid.res {
let span = mac_attr.span.clone();
self.imports.push((lcx.tcx.def_path_str(mac_id), span));
}
}
} where this would happenfn check_crate_post(&mut self, lcx: &LateContext<'_, '_>, _krate: &hir::Crate<'_>) {
for (import, span) in self.imports.iter() {
let matched = self
.mac_refs
.iter()
.find(|(_span, mac)| import.ends_with(&mac.name))
.is_some();
if matched {
self.mac_refs.retain(|(_span, mac)| !import.ends_with(&mac.name));
let msg = "`macro_use` attributes are no longer needed in the Rust 2018 edition";
let help = format!("use {}", import);
span_lint_and_sugg(
lcx,
MACRO_USE_IMPORTS,
*span,
msg,
"remove the attribute and import the macro directly, try",
help,
Applicability::HasPlaceholders,
)
}
}
if !self.mac_refs.is_empty() {
// TODO if not empty we found one we could not make a suggestion for
// such as std::prelude::v1 or something else I haven't thought of.
// If we defer the calling of span_lint_and_sugg we can make a decision about its
// applicability?
}
} |
☔ The latest upstream changes (presumably #5319) made this pull request unmergeable. Please resolve the merge conflicts. |
a46ff1f
to
b9a5d9f
Compare
☔ The latest upstream changes (presumably #5294) made this pull request unmergeable. Please resolve the merge conflicts. |
d611683
to
81048cd
Compare
@DevinR528 Sorry for taking so long for the review. I don't have an excuse, just wasn't really motivated to review PRs recently. I didn't forget this PR and want to review it in the coming days. |
☔ The latest upstream changes (presumably #5470) made this pull request unmergeable. Please resolve the merge conflicts. |
81048cd
to
4f1698f
Compare
☔ The latest upstream changes (presumably #5423) made this pull request unmergeable. Please resolve the merge conflicts. |
4f1698f
to
e535b66
Compare
☔ The latest upstream changes (presumably #5141) made this pull request unmergeable. Please resolve the merge conflicts. |
e535b66
to
71896a1
Compare
☔ The latest upstream changes (presumably #5332) made this pull request unmergeable. Please resolve the merge conflicts. |
71896a1
to
4fdf54b
Compare
☔ The latest upstream changes (presumably #5522) made this pull request unmergeable. Please resolve the merge conflicts. |
6beb998
to
d273ee8
Compare
☔ The latest upstream changes (presumably #5550) made this pull request unmergeable. Please resolve the merge conflicts. |
Is this compile failure something I can fix?
|
#5566 We're currently figuring out some stuff with the move to |
stderr differs? That is weird 🤔 |
☔ The latest upstream changes (presumably #5378) made this pull request unmergeable. Please resolve the merge conflicts. |
It's the ordering of the suggestions, hmmm I'll look into this... |
cargo dev update lints use if_chain clean up alot, span_lint_and_sugg find imported macros for sugg
how to compare macro to import path add more imports to test
remove stdout, fix clippy warnings, fmtcar
@bors r+ Thanks! |
📌 Commit 3f91e39 has been approved by |
Macro use sugg changelog: Add auto applicable suggstion to macro_use_imports fixes #5275 <s>Where exactly is the `wildcard_imports_helper` I haven't been able to import anything ex. `use lazy_static;` or something like for that I get version/compiler conflicts?</s> Found it. Should we also check for `#[macro_use] extern crate`, although this is still depended on for stuff like `rustc_private`?
💔 Test failed - checks-action_test |
Wait the problem is, that the output is different on 32bit linux 🤔 Since only the order is different, you can add |
You also have to update the |
Oops duh 🤦 |
Thanks! @bors r+ |
📌 Commit e521a4e has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
changelog: Add auto applicable suggstion to macro_use_imports
fixes #5275
Where exactly is the
wildcard_imports_helper
I haven't been able to import anything ex.use lazy_static;
or something like for that I get version/compiler conflicts?Found it.
Should we also check for
#[macro_use] extern crate
, although this is still depended on for stuff likerustc_private
?