-
Notifications
You must be signed in to change notification settings - Fork 12
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
Handle macros in lints #263
Draft
DaAlbrecht
wants to merge
9
commits into
TheBevyFlock:main
Choose a base branch
from
DaAlbrecht:167-macros
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
79110a5
feat: test external macros, insert_event_resource
DaAlbrecht 2678c89
feat: add macro
DaAlbrecht 96c0dbe
feat: skip all expr that come from an external maco in insert_event_r…
DaAlbrecht cc4de15
feat: main_return_without_appexit
DaAlbrecht 127ff5f
skip external macros for missing_reflect
DaAlbrecht 09c726e
feat: queries
DaAlbrecht 60d70dc
feat: panicking_methods
DaAlbrecht 9aaf92a
feat: plugin_not_ending_in_plugin
DaAlbrecht 6e2820c
feat: insert_unit_bundle
DaAlbrecht File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
//! Utility to test the behavior of lints when the code originates from an external macro. | ||
//! | ||
//! From: https://github.com/rust-lang/rust-clippy/blob/master/tests/ui/auxiliary/proc_macros.rs | ||
extern crate proc_macro; | ||
use proc_macro::{ | ||
token_stream::IntoIter, | ||
Delimiter::{self, Parenthesis}, | ||
Group, Ident, Literal, Punct, | ||
Spacing::{self, Alone}, | ||
Span, TokenStream, TokenTree as TT, | ||
}; | ||
|
||
type Result<T> = core::result::Result<T, TokenStream>; | ||
|
||
/// Token used to escape the following token from the macro's span rules. | ||
const ESCAPE_CHAR: char = '$'; | ||
|
||
/// Takes a sequence of tokens and return the tokens with the span set such that they appear to be | ||
/// from an external macro. Tokens may be escaped with either `$ident` or `$(tokens)`. | ||
#[proc_macro] | ||
pub fn external(input: TokenStream) -> TokenStream { | ||
let mut res = TokenStream::new(); | ||
if let Err(e) = write_with_span(Span::mixed_site(), input.into_iter(), &mut res) { | ||
e | ||
} else { | ||
res | ||
} | ||
} | ||
|
||
/// Make a `compile_error!` pointing to the given span. | ||
fn make_error(msg: &str, span: Span) -> TokenStream { | ||
TokenStream::from_iter([ | ||
TT::Ident(Ident::new("compile_error", span)), | ||
TT::Punct(punct_with_span('!', Alone, span)), | ||
TT::Group({ | ||
let mut msg = Literal::string(msg); | ||
msg.set_span(span); | ||
group_with_span( | ||
Parenthesis, | ||
TokenStream::from_iter([TT::Literal(msg)]), | ||
span, | ||
) | ||
}), | ||
]) | ||
} | ||
|
||
/// Copies all the tokens, replacing all their spans with the given span. Tokens can be escaped | ||
/// either by `$ident` or `$(tokens)`. | ||
fn write_with_span(s: Span, mut input: IntoIter, out: &mut TokenStream) -> Result<()> { | ||
while let Some(tt) = input.next() { | ||
match tt { | ||
TT::Punct(p) if p.as_char() == ESCAPE_CHAR => { | ||
expect_tt( | ||
input.next(), | ||
|tt| match tt { | ||
tt @ (TT::Ident(_) | TT::Literal(_)) => { | ||
out.extend([tt]); | ||
Some(()) | ||
} | ||
TT::Punct(mut p) if p.as_char() == ESCAPE_CHAR => { | ||
p.set_span(s); | ||
out.extend([TT::Punct(p)]); | ||
Some(()) | ||
} | ||
TT::Group(g) if g.delimiter() == Parenthesis => { | ||
out.extend([TT::Group(group_with_span( | ||
Delimiter::None, | ||
g.stream(), | ||
g.span(), | ||
))]); | ||
Some(()) | ||
} | ||
_ => None, | ||
}, | ||
"an ident, a literal, or parenthesized tokens", | ||
p.span(), | ||
)?; | ||
} | ||
TT::Group(g) => { | ||
let mut stream = TokenStream::new(); | ||
write_with_span(s, g.stream().into_iter(), &mut stream)?; | ||
out.extend([TT::Group(group_with_span(g.delimiter(), stream, s))]); | ||
} | ||
mut tt => { | ||
tt.set_span(s); | ||
out.extend([tt]); | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn expect_tt<T>( | ||
tt: Option<TT>, | ||
f: impl FnOnce(TT) -> Option<T>, | ||
expected: &str, | ||
span: Span, | ||
) -> Result<T> { | ||
match tt { | ||
None => Err(make_error( | ||
&format!("unexpected end of input, expected {expected}"), | ||
span, | ||
)), | ||
Some(tt) => { | ||
let span = tt.span(); | ||
match f(tt) { | ||
Some(x) => Ok(x), | ||
None => Err(make_error( | ||
&format!("unexpected token, expected {expected}"), | ||
span, | ||
)), | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn punct_with_span(c: char, spacing: Spacing, span: Span) -> Punct { | ||
let mut p = Punct::new(c, spacing); | ||
p.set_span(span); | ||
p | ||
} | ||
|
||
fn group_with_span(delimiter: Delimiter, stream: TokenStream, span: Span) -> Group { | ||
let mut g = Group::new(delimiter, stream); | ||
g.set_span(span); | ||
g | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not sure why rustfix does not fix the macro.. the span is correct but the suggestion is not applied