Skip to content
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

Merged
merged 15 commits into from
Oct 16, 2023

Conversation

disconnect3d
Copy link
Member

No description provided.

@smoelius
Copy link
Collaborator

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.

Copy link
Collaborator

@smoelius smoelius left a 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 Show resolved Hide resolved
@@ -37,6 +41,7 @@ members = [
"env_cargo_path",
"non_local_effect_before_error_return",
"non_thread_safe_call_in_test",
"incorrect_matches_operation",
Copy link
Collaborator

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
Copy link
Collaborator

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);
}
Copy link
Collaborator

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! {
Copy link
Collaborator

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.

Copy link
Member Author

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 😅 .

Copy link
Collaborator

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
}
Copy link
Collaborator

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...
Copy link
Collaborator

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, _))

Copy link
Member Author

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

Copy link
Collaborator

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]]
Copy link
Collaborator

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.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// Checks for inefficient or incorrect use of matches! macro.
/// Checks for inefficient or incorrect use of the `matches!` macro.

@disconnect3d disconnect3d force-pushed the add-incorrect-matches-operation branch from 50ae44c to 2f48767 Compare October 11, 2023 10:38
@disconnect3d
Copy link
Member Author

I rebased this branch on top of latest master.

@smoelius smoelius force-pushed the add-incorrect-matches-operation branch from 97a1605 to aa4ef1c Compare October 16, 2023 10:14
@smoelius smoelius added this pull request to the merge queue Oct 16, 2023
Merged via the queue into master with commit 00bbda3 Oct 16, 2023
12 checks passed
@smoelius smoelius deleted the add-incorrect-matches-operation branch October 16, 2023 13:39
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.

2 participants