-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Improve the cognitive_complexity lint to show what lines are causing the complexity #4470
Comments
cc @felix91gr That's a good idea IMO! But we have to be careful to not spam the output, so that the actual message can't be found anymore. If we implement this feature, I'd suggest to hide it behind a configuration / attribute, e.g.
|
@flip1995 Good point! I think it is also a good idea to follow the behaviour of #[clippy::cognitive_complexity_verbosity = "full"] |
I have almost finished working on this. So please don't start working on this one.
@flip1995 According to the code in the |
How do I know if some if true && false && true && false && true && false Every span of Now I have this printed instead, what is incorrect:
What is also questionable is that if we are using some constants there, then the user still has the cognitive complexity of 6, because he doesn't know the values of constants: IS_X && IS_Y && IS_Z && IS_T && IS_V && IS_W && IS_H This |
Is your implementation based on the implementation on the Clippy master branch or the PR #3963*? Either way I don't really know how the CoC lint works / is implemented. In #3963, the CoC lint is a visitor implementation, that increases the CoC by visiting expressions (IIUC). We could then add code like this: cb = coc_before();
walk_expr(expr);
ca = coc_after();
some_structure.store((ca - cb, expr.span)); When the CoC calculation is finished (and the lint is triggered) we can produce notes: span_lint_and_then(.., |db| {
for (coc_inc, expr) in some_structure {
if coc_inc > threshold {
db.span_note(.., "this code increases the CoC by {}");
}
}
}); Since functions with high CoC, are most of the time large, building As I stated above, I don't really know, if what I suggested is possible or not. @felix91gr can help you here more (in case he got some time on his hands). * IMO you should implement this based on #3963 and create a PR on the branch of the PR, instead of the master branch. Otherwise, either your work has to be redone completely, once #3963 gets merged or will be discarded in favor of the CoC MVP. |
Thank you very much for the information! I'll rebase my PR on top of #3963 one. |
We definitely know, if the verbosity is set or not, so we can store every expression or none at all, pretty easily. If this could be implemented, like I suggested above, you can check after visiting an expression if the complexity of the expression exceeds the threshold, by just checking |
Oh, excuse me, was reading between the lines. Yes, sure, we can do this. |
We could improve the
cognitive_complexity
lint further by showing the^
sign under tokens which cause the increment of cognitive complexity. Doing so would let users know what actually was causing the complexity and so they would spend their time more effectively fixing the issue.Right now, for people unfamiliar with the cognitive complexity, it is impossible to know for sure what is causing problems in their code, they have to study the paper. Also, even people who had it known before the problem appeared, could simply have forgotten it. This explicit show will not only help them understand what causes a problem here but also will teach them.
The text was updated successfully, but these errors were encountered: