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

Move unimplemented to an EarlyLintPass #4186

Closed

Conversation

maxencefrenette
Copy link
Contributor

@maxencefrenette maxencefrenette commented Jun 8, 2019

fixes #3528
WIP

Hi all ! As suggested in the issue thread, I moved the unimplemented lint to an EarlyLintPass. It works great and makes the code much simpler. However, I don't know how to move the panic lint to an EarlyLintPass. I see a couple options right now.

  • Keep the LateLintPass for it and move it to another file.
  • Adapt the check_tts() function from clippy_lints/src/write.rs. However, I couldn't fully grasp how to adapt it to work in this case.
  • Something much simpler that I'm not seeing right now.

@flip1995
Copy link
Member

flip1995 commented Jun 8, 2019

The check_tts function of write.rs is way to complex for this use case, but as you noticed correctly you can copy some code from there.

You basically just need this part of the check_tts function:

let mut parser = parser::Parser::new(&cx.sess.parse_sess, tts, None, false, false, None);

let (fmtstr, fmtstyle) = match parser.parse_str().map_err(|mut err| err.cancel()) {
Ok((fmtstr, fmtstyle)) => (fmtstr.to_string(), fmtstyle),
Err(_) => return (None, expr),
};

For panic!("foo {}", a), this will parse you the string "foo {}", which you can then examine with the code

let string = string.as_str().replace("{{", "").replace("}}", "");
if let Some(par) = string.find('{');
if string[par..].contains('}');

This will tell you if there is a {} in the string.

After this, you can check if there are parameters with

if parser.expect(&token::Comma).map_err(|mut err| err.cancel()).is_err() {
return (None, expr);
}

and

// You only need to try to parse one expression and not all format parameters
parser.parse_expr().map_err(|mut err| err.cancel()).is_some()

You pretty much can put this all together in one if_chain!

@ghost
Copy link

ghost commented Jun 9, 2019

Did the problem with EarlyLintExpansion and nested macros get fixed? I don't think we should use pre-expansion lints at all until that's resolved.

@flip1995
Copy link
Member

flip1995 commented Jun 9, 2019

The problem got not fixed and probably won't be fixed.

cc #3217 (comment)

We should limit PreExpansionPass to lints that really only look for a specific macro call.

This is the case here. So it should be fine to turn this into a PreExpansionPass

@maxencefrenette Could you add following test:

macro_rules! outer_macro {
    ($e:expr) => {
        $e;
    };
}

// ...
outer_macro!(unimplemented!());

This unimplemented! call should get linted. If that's not the case the problem @mikerite described is more severe than I thought.

@maxencefrenette
Copy link
Contributor Author

The problem got not fixed and probably won't be fixed.

cc #3217 (comment)

We should limit PreExpansionPass to lints that really only look for a specific macro call.

This is the case here. So it should be fine to turn this into a PreExpansionPass

@maxencefrenette Could you add following test:

macro_rules! outer_macro {
    ($e:expr) => {
        $e;
    };
}

// ...
outer_macro!(unimplemented!());

This unimplemented! call should get linted. If that's not the case the problem @mikerite described is more severe than I thought.

Interesting stuff. I'll for sure add that test when I get back to it. Thanks a lot for your help @flip1995 !

@flip1995 flip1995 added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Jun 12, 2019
@maxencefrenette
Copy link
Contributor Author

Everything passes locally and I updated the .stderr for the test. @mikerite @flip1995 what you feared happened. It doesn't detect nested macros, as can be seen in the test files.

@flip1995 flip1995 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 17, 2019
@flip1995
Copy link
Member

flip1995 commented Jun 19, 2019

It doesn't detect nested macros, as can be seen in the test files.

Well that's bad :( I'm not sure what's the best course of action here is... Let's be naive: Can we use the check_mac function in an EarlyLintPass after macro expansion?

@bors
Copy link
Collaborator

bors commented Jul 3, 2019

☔ The latest upstream changes (presumably #4251) made this pull request unmergeable. Please resolve the merge conflicts.

@flip1995
Copy link
Member

flip1995 commented Jan 8, 2020

Thanks for the work! But I don't think moving this lint to a pre_expansion_pass is the right thing to do here. Since this is stale for some time, I'm going ahead and close this PR.

@flip1995 flip1995 closed this Jan 8, 2020
@flip1995 flip1995 added S-inactive-closed Status: Closed due to inactivity and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jan 8, 2020
@HKalbasi
Copy link
Member

finished in #5811
@rustbot label -S-inactive-closed

@rustbot rustbot removed the S-inactive-closed Status: Closed due to inactivity label Jul 29, 2021
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.

clippy::unimplemented doesn't detect unimplemented!() with message
5 participants