-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add incorrect-matches-operation example #879
Conversation
I'm currently fixing some CI failures attributable to the Rust 1.73 release. So, you may not be able to fix some failures for your PR until I am done with that. Apologies. |
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.
You will need to rebase to get CI to pass.
Please forgive me if I need to make an additional pass, but I think this looks pretty good.
examples/general/Cargo.toml
Outdated
@@ -37,6 +41,7 @@ members = [ | |||
"env_cargo_path", | |||
"non_local_effect_before_error_return", | |||
"non_thread_safe_call_in_test", | |||
"incorrect_matches_operation", |
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.
Let's please keep this list sorted.
@@ -0,0 +1 @@ | |||
/target |
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.
Please delete this file. (A top-level .gitignore does its job.)
|
||
// This one will not error out | ||
let _c = matches!(x, 2) | matches!(_b, true); | ||
} |
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.
Nit: please add the EOL.
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
|
||
|
||
dylint_linting::impl_pre_expansion_lint! { |
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.
Since you don't need to store anything, you should be able to use declare_pre_expansion_lint
and avoid having to declare IncorrectMatchesOperation
manually.
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.
Fwiw the linked docs should probably give example on how to use 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.
Your point is very well taken.
#[derive(Default)] | ||
pub struct IncorrectMatchesOperation { | ||
// We don't need to store any state for this lint | ||
} |
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.
As per above, it should be possible to remove this declaration.
|
||
/// Returns whether a given token is a comma | ||
fn is_comma_token(tree: &TokenTree) -> bool { | ||
// TODO/FIXME: This can probably be written with some kind of matches! macro, but I don't know how... |
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.
Maybe this?
matches!(tree, TokenTree::Token(TokenKind::Comma, _))
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.
error[E0308]: mismatched types
--> incorrect_matches_operation/src/lib.rs:101:37
|
101 | matches!(tree, TokenTree::Token(TokenKind::Comma, _))
| ---- ^^^^^^^^^^^^^^^^ expected `Token`, found `TokenKind`
| |
| this expression has type `&TokenTree`
101 | matches!(tree, TokenTree::Token(Token(TokenKind::Comma, _), _))
| ^^^^^ not found in this scope
|
help: consider importing one of these items
|
7 + use crate::TokenTree::Token;
|
7 + use rustc_ast::tokenstream::AttrTokenTree::Token;
|
7 + use rustc_ast::tokenstream::TokenTree::Token;
error[E0308]: mismatched types
--> incorrect_matches_operation/src/lib.rs:101:37
|
101 | matches!(tree, TokenTree::Token(TokenTree::Token(TokenKind::Comma, _), _))
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Token`, found `TokenTree`
| |
| this expression has type `&TokenTree`
error[E0308]: mismatched types
--> incorrect_matches_operation/src/lib.rs:101:54
|
101 | matches!(tree, TokenTree::Token(TokenTree::Token(TokenKind::Comma, _), _))
| ---- ^^^^^^^^^^^^^^^^ expected `Token`, found `TokenKind`
| |
| this expression has type `&TokenTree`
error[E0308]: mismatched types
--> incorrect_matches_operation/src/lib.rs:101:37
|
101 | ...s!(tree, TokenTree::Token(rustc_ast::tokenstream::AttrTokenTree::Token(TokenKind::Comma, _), ...
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Token`, found `AttrTokenTree`
| |
| this expression has type `&TokenTree`
error[E0308]: mismatched types
--> incorrect_matches_operation/src/lib.rs:101:82
|
101 | ...s!(tree, TokenTree::Token(rustc_ast::tokenstream::AttrTokenTree::Token(TokenKind::Comma, _...
| ---- this expression has type `&TokenTree` ^^^^^^^^^^^^^^^^ expected `Token`, found `TokenKind`
...I am not sure how to do this lol
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.
Sorry, I overlooked the .kind
. This should do it:
matches!(tree, TokenTree::Token(Token { kind: TokenKind::Comma, .. }, _))
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[[example]] |
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.
Because you're using dylint_testing::ui_test
(not dylint_testing::ui_test_example
), this example should not be necessary.
|
||
dylint_linting::impl_pre_expansion_lint! { | ||
/// ### What it does | ||
/// Checks for inefficient or incorrect use of matches! macro. |
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.
/// Checks for inefficient or incorrect use of matches! macro. | |
/// Checks for inefficient or incorrect use of the `matches!` macro. |
50ae44c
to
2f48767
Compare
I rebased this branch on top of latest master. |
75fcebe
to
97a1605
Compare
Co-authored-by: Samuel Moelius <[email protected]>
97a1605
to
aa4ef1c
Compare
No description provided.