-
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
Initial implementation of possible shortcircuiting collect lint #2077
Conversation
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.
I'd say we go with an allow by default lint until we've run it on a few codebases
tests/ui/collect.stderr
Outdated
26 | let _result: Vec<_> = a.iter().map(Some).collect(); | ||
| ^^^^^^ | ||
| | ||
help: if you are only interested in the `Some` values or the first `None`, try |
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.
The message about the first None seems a little weird
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.
Agreed. Fixed.
clippy_lints/src/collect.rs
Outdated
error_variant: "None", | ||
}) | ||
} else if match_type(cx, normal_ty, &paths::RESULT) { | ||
Some(Suggestion { |
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.
Why are these lowercase?
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.
Addressed.
ping @marcusklaas |
Hi! Sorry about leaving this for such a long time. Given this lint's inherent propensity of generating false positives, maybe it's better to just close this? |
I'd have added it as allow-by-default for now. I like the idea, and maybe we can run it and figure out in which cases we don't want to lint and add them as exceptions |
Okay. That sounds fair. I will touch up this PR then! |
d18648e
to
35cbf2f
Compare
Build failure seems unrelated to PR. Master branch also fails to build. |
Could you rebase this and use the declare_clippy_lint macro and make this lint a nursery lint? |
@marcusklaas ping |
The code appears to be broken and I do not know nearly enough about the compiler internals to fix it.
This PR is best closed I think.
…On 7 Jun 2018, 12:21, at 12:21, "Mateusz Mikuła" ***@***.***> wrote:
@marcusklaas ping
--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
#2077 (comment)
|
Does somebody else want to take over? idk what the brokenness is, but it would be a shame to let this PR go to waste! It was pretty close to merging already during it's history, we should be able to get it there again. |
Don't feel bad about it. I had fun building it. And it may only have limited usefulness.
…On 8 Jun 2018, 01:19, at 01:19, Oliver Schneider ***@***.***> wrote:
Does somebody else want to take over? idk what the brokenness is, but
it would be a shame to let this PR go to waste! It was pretty close to
merging already during it's history, we should be able to get it there
again.
--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
#2077 (comment)
|
Rebased it and fixed a few bugs, which caused ICEs in the |
This lint still produces an ICE in rustfmt (travis). This error is produced by this line: |
Judging by the ICE message and the documentation of TyCtxt.normalize_erasing_regions, I think it's safe to say that the "normalization failed" path was hit. Possibly somewhere in rustc-rayon? |
Rebased it on master again, let's see if it still fails |
clippy_lints/src/collect.rs
Outdated
@@ -69,11 +75,11 @@ struct Suggestion { | |||
|
|||
fn format_suggestion_pattern<'a, 'tcx>( | |||
cx: &LateContext<'a, 'tcx>, | |||
collection_ty: &TypeVariants, | |||
collection_ty: &TyKind<'_>, |
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.
TyKind
should never be used directly, you can write Ty
instead.
@Manishearth @oli-obk I think clippy might have a general problem wrt this.
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.
another box for rust-lang/rust#49509
clippy_lints/src/collect.rs
Outdated
@@ -84,7 +90,7 @@ fn format_suggestion_pattern<'a, 'tcx>( | |||
|
|||
buf | |||
}, | |||
TypeVariants::TyParam(p) => p.to_string(), | |||
TyKind::Param(p) => p.to_string(), |
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 should write these as e.g. ty::Param
.
Removed the usage of The ICE in rustfmt/rls is still there and it's caused by an incorrect use of rust-clippy/clippy_lints/src/collect.rs Lines 124 to 130 in 4f69870
I'm not sure what exactly needs to be fixed, but I will try to fix it later today. |
☔ The latest upstream changes (presumably #3529) made this pull request unmergeable. Please resolve the merge conflicts. |
ping from triage @phansch: Any updates on this? It seems that this just needs a stderr update. |
thanks @flip1995, that's news to me. I think I got stuck on fixing the ICE last time but it indeed appears to be gone. I will have another look today. |
So, currently compiling |
☔ The latest upstream changes (presumably #3603) made this pull request unmergeable. Please resolve the merge conflicts. |
☔ The latest upstream changes (presumably #3600) made this pull request unmergeable. Please resolve the merge conflicts. |
☔ The latest upstream changes (presumably #3968) made this pull request unmergeable. Please resolve the merge conflicts. |
half yearly ping @phansch. Should we |
Yeah, makes sense! I currently don't have time to continue this. IIRC, if someone wants to continue this, they should try to find a minimal repro of the ICE that doesn't require |
This lint detects collect calls that generate collections of
Option
orResult
and suggests instead collect into aOption<C>
orResult<C, _>
respectively, which has personally almost always been what I intend to do. Since this shortcircuiting feature of collect is not very discoverable, I believe this lint has some value.There may be cases where one really does want a collection of result/ option, in which case this lint will produce a false positive. How should we deal with those? Below is a non-exhaustive list of options:
Result<_, E>
)Oh, and mega thanks to @eddyb for his gracious guidance through the labyrinths of the rust compiler on the rust IRC!