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

Ignore cognitive complexity in macro expanded code #3900

Closed
bodil opened this issue Mar 21, 2019 · 13 comments
Closed

Ignore cognitive complexity in macro expanded code #3900

bodil opened this issue Mar 21, 2019 · 13 comments
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy L-complexity Lint: Belongs in the complexity lint group

Comments

@bodil
Copy link

bodil commented Mar 21, 2019

Would it be possible to run the cognitive/cyclomatic complexity lints before macro expansion, or otherwise have them ignore macro output? I've got a number of macros with pretty heavy code generation, and I invariably have to turn off these lints in order to avoid triggering them even for macros that are very simple from the user's perspective.

@bodil bodil changed the title Ignore cognitive complexity in macros Ignore cognitive complexity in macro expanded code Mar 21, 2019
@flip1995 flip1995 added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages L-complexity Lint: Belongs in the complexity lint group labels Mar 21, 2019
@oli-obk
Copy link
Contributor

oli-obk commented Mar 22, 2019

@felix91gr I think we had discussions about this, right? Like moving the cognitive complexity lint to a pre-expansion pass so macro invocations are just as "complex" as a single function call.

@felix91gr
Copy link
Contributor

@oli-obk yes we did indeed :)

@bodil, we're planning on doing a rewrite of the Cognitive Complexity lint soon. We will convert it to a pre-expansion lint, and among other things this is precisely one of the changes we have planned :)

@felix91gr
Copy link
Contributor

I've added this to the list of (Nesting-Indepentend) Increments constructs, right where function calls are: #3793 (comment) :)

@yaahc
Copy link
Member

yaahc commented Aug 29, 2019

Should this lint be disabled in the interim? tracing in particular is falling prey to it because their macros introduce branches and so if you do a bit of logging in your function suddenly clippy shouts at you.

Also, according to @hawkw log should also run into this issue.

Here's an egregious example of a gist that triggers cognitive complexity

https://gist.github.com/jarrodldavis/c6ba356b4243b898a658e27f9042a483

@felix91gr
Copy link
Contributor

@yaahc maybe. I'm sorry for what's happening. The plan was for the changes to be finished around April or so, but life happened and I'm on a hiatus for the moment.

Since the old version (the one currently on master) is a post-macro-expansion pass, I don't think a quick fix on the old version can help here.

Maybe downgrading the existing one to nursery is an option, given that we'll do that regardless after we merge the new version. We plan on doing so for fine-tuning of the new lint's defaults, and therefore doing so right now would probably be fine, I think?

@Centril @oli-obk @Manishearth what do you think of that?

@felix91gr
Copy link
Contributor

And fwiw, the short version of why we want to ultimately not check the post-expansion code in the lint, is because the lint checks how hard to grasp is a piece of code. Since macros do introduce abstraction, they are not "weightless". But they are cognitively (we believe) very close to reading a function call.
Therefore we score the pre-expansion code in the new version :)

@yaahc
Copy link
Member

yaahc commented Aug 29, 2019

That's exactly what I would expect. A macro abstracts code in the exact way a function does, it's only the underlying implementation that is different.

Downgrading to nursery sounds good 👍

@phansch phansch added the good-first-issue These issues are a good way to get started with Clippy label Aug 30, 2019
@flip1995
Copy link
Member

flip1995 commented Aug 30, 2019

Therefore we score the pre-expansion code in the new version

We need to think about a case like this though :

some_macro!({
    // Some complex expression
} ) 

Because in a pre expansion pass, we can't access the macro arg expressions, since they're not parsed by that point. Maybe we could first run a pre expansion parse, but don't lint there, but calculate the CoC and mark the spans of the macros that need to be rechecked. After that, we run an early lint pass and recheck these spans. Not sure how to handle macro in macro situations though.

@felix91gr
Copy link
Contributor

If I remember correctly, @flip1995, we are walking inside all expressions, including the ones inside macro invocations. So we should be scoring whatever they have inside.

What we're not scoring, however, is the post-macro-expansion code, which since it's abstracted away while a user is reading the invocation code, it's ignored in the score.

That is however, a good reminder. I will probably remember to double check this before we ship it, thanks :3

@flip1995
Copy link
Member

I doubt that. We have a PR open with this exact same problem, where we're kind of stuck, since there is no good solution for this (yet?). See #4186 (comment)

So if you found a way to check them, I would be really happy about it, since this would resolve a lot of problems with linting macro code. But make sure to check this. (Maybe I get to write a test for this. Your CoC PR is already a pre-expansion pass, isn't it?)

@felix91gr
Copy link
Contributor

Your CoC PR is already a pre-expansion pass, isn't it?

It is! Do take a look, and feel free to make tests for it. We definitely need more tests before calling it done :3

@Manishearth
Copy link
Member

Mostly ambivalent about moving it to nursery.

@phansch
Copy link
Member

phansch commented Apr 26, 2020

The lint has been moved to the nursery a while ago. Since the macro handling case is now tracked in #3793, I'm going to go ahead and close this issue.

@phansch phansch closed this as completed Apr 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy L-complexity Lint: Belongs in the complexity lint group
Projects
None yet
Development

No branches or pull requests

7 participants