-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Recommend fix count()
-> len()
on slices
#87614
Conversation
r? @jackh726 (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
6a19751
to
a721806
Compare
My first thought is (without reading too much of the code): what other methods do we have suggestions like this for? Also, even though this is technically compiler code, it seems more like a @rust-lang/libs decision; it reminds me somewhat of the rustdoc aliases: at what point is it "okay" and when is it "too much" |
The same file I modified also has a suggestion for |
Could this use |
Here's what I tried, to achieve the effect using that system. It didn't work. diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs
index a1a336a0574..bdee0e202c6 100644
--- a/library/core/src/iter/traits/iterator.rs
+++ b/library/core/src/iter/traits/iterator.rs
@@ -25,6 +25,13 @@ fn _assert_is_object_safe(_: &dyn Iterator<Item = ()>) {}
/// [impl]: crate::iter#implementing-iterator
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_on_unimplemented(
+ on(
+ all(
+ any(_Self = "[]", _Self = "std::vec::Vec", _Self = "std::string::String", _Self = "str"),
+ from_method = "count",
+ ),
+ label = "consider using `.len()`"
+ ),
on(
_Self = "std::ops::RangeTo<Idx>",
label = "if you meant to iterate until a value, add a starting value", Here's the message that it gives:
I notice that my changes have no effect on it, but also, that the |
My hunch is that the |
No. |
I'm going to defer to libs (or libs-api?) here. r? @cuviper feel free to reassign |
☔ The latest upstream changes (presumably #88556) made this pull request unmergeable. Please resolve the merge conflicts. |
@notriddle Please rebase instead of merging in upstream changes because of the "No-Merge Policy". Thanks! |
bb9c4c7
to
5b78c55
Compare
This feels like a fairly reasonable change to me (obviously, we can iterate further on the message in the future). I'm not entirely convinced at whether this is a particularly common case, but the extra work is not huge, and so it seems like an OK change to make in my opinion. My cursory look at the compiler code suggests we don't consider on_unimplemented for this kind of error, so we might want to implement that in the future -- but it seems unnecessary to block this improvement on that larger goal. I also agree with others on the "is this too much" angle, but ultimately, this is something relatively easy to back out and we're not yet seeing a huge influx of such targeted messages that I'm worried. :) That said, I think the proposed error message in this PR is pretty suboptimal -- we're hiding the trait the user might've been intending to reference, not even mentioning its name:
I think it would be good to rework this to something like:
Or potentially to hide the "unsatisfied due to trait bounds" error altogether, though not sure on that front. cc @rust-lang/wg-diagnostics -- wondering if you have opinions on this particular case or if we should instead invest in expanding rustc_on_unimplemented to cover cases like this and avoid ad-hoc code in the compiler. |
I agree. Rebased and pushed a new error message, that's closer to what you wrote.
|
5b78c55
to
630457e
Compare
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.
r=me with nit fixed
630457e
to
6a17ee6
Compare
@rustbot label: -S-waiting-on-author +S-waiting-on-review |
@bors r+ rollup |
📌 Commit 6a17ee6 has been approved by |
…rk-Simulacrum Recommend fix `count()` -> `len()` on slices Fixes rust-lang#87302
…askrgr Rollup of 10 pull requests Successful merges: - rust-lang#87614 (Recommend fix `count()` -> `len()` on slices) - rust-lang#91065 (Add test for evaluate_obligation: Ok(EvaluatedToOkModuloRegions) ICE) - rust-lang#91312 (Fix AnonConst ICE) - rust-lang#91341 (Add `array::IntoIter::{empty, from_raw_parts}`) - rust-lang#91493 (Remove a dead code path.) - rust-lang#91503 (Tweak "call this function" suggestion to have smaller span) - rust-lang#91547 (Suggest try_reserve in try_reserve_exact) - rust-lang#91562 (Pretty print async block without redundant space) - rust-lang#91620 (Update books) - rust-lang#91622 (:arrow_up: rust-analyzer) Failed merges: - rust-lang#91571 (Remove unneeded access to pretty printer's `s` field in favor of deref) r? `@ghost` `@rustbot` modify labels: rollup
Fixes #87302