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

Fix: fixed multipart_suggestion in index_refutable_slice uitest #13727

Merged

Conversation

scottgerring
Copy link
Contributor

@scottgerring scottgerring commented Nov 25, 2024

This should address #13099 for the derivable_impls test. As this combines everything into a single multipart_suggestion, the feedback message is a little less "targeted" than it was before, but now it provides a complete--fixable suggestion - e.g.:

error: this binding can be a slice pattern to avoid indexing
  --> tests/ui-toml/max_suggested_slice_pattern_length/index_refutable_slice.rs:5:17
   |
LL |     if let Some(slice) = slice {
   |                 ^^^^^
   |
note: the lint level is defined here
  --> tests/ui-toml/max_suggested_slice_pattern_length/index_refutable_slice.rs:1:9
   |
LL | #![deny(clippy::index_refutable_slice)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: replace the binding and indexed access with a slice pattern
   |
LL ~     if let Some([_, _, _, _, _, _, _, slice_7, ..]) = slice {
LL |
LL |         // This would usually not be linted but is included now due to the
LL |         // index limit in the config file
LL ~         println!("{}", slice_7);
   |

changelog: [index_refutable_slice]: Fixed multipart_suggestions to provide correct rustfix-able lint

@rustbot
Copy link
Collaborator

rustbot commented Nov 25, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @y21 (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 25, 2024
Applicability::MaybeIncorrect,
"replace the binding and indexed access with a slice pattern",
suggestions,
Applicability::MachineApplicable,
Copy link
Contributor Author

@scottgerring scottgerring Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this as the --fix works now for the included test cases

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: This level is not about if --fix is possible or not, but rather whether the suggestion is always correct. I don't know the reason, why it was set to MaybeIncorrect and this might actually be MachineApplicable. As you looked into this lint: Can you think of cases where applying the suggestions by those lints might break (or change semantics) of the code?

Copy link
Member

@y21 y21 Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could argue that the suggestion is always a semantic change as it's removing a panic possibly intended to act as an assertion (for when an in-bounds index is an invariant), e.g. when a slice being non-empty is an invariant of a function then changing x[0] to if let [x_0, ..] = x doesn't make much sense as it only delays the bug or makes the bug go unnoticed.

I'm also not sure about the introduced names. slice_7 seems like a meaningless name to introduce and IMO it would make more sense to just have the user fix it manually and give the bindings proper names

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I would keep it as MaybeIncorrect at least, maybe as HasPlaceholders even. Leaning towards the former.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey both - thanks for the detailed feedback!

Reading up about the applicability, it does seem like MaybeIncorrect is where it should stay - it produces valid code, but may not be what the user intended - either because the placeholder names aren't really immediately useful, or because the user is intentionally using this as a mechanism to panic early as @y21 suggests.

HasPlaceholders seems off as it does produce valid code - although the dummy field names do otherwise seem quite placeholder-ish!

     /// The suggestion contains placeholders like `(...)` or `{ /* fields */ }`. The suggestion
     /// cannot be applied automatically because it will not result in valid Rust code. The user
    /// will need to fill in the placeholders.

I'll fix this up and check the other files this test touches too.

@scottgerring
Copy link
Contributor Author

@rustbot review

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also touches

  • tests/ui/index_refutable_slice/if_let_slice_binding.stderr
  • tests/ui/index_refutable_slice/slice_indexing_in_macro.stderr

Which are also part of the list in the tracking issue. Can you try to remove the no-rustfix comment there too and if that also Just Work ™️ with your fixes in this PR?

@scottgerring scottgerring requested review from y21 and flip1995 November 29, 2024 11:29
@scottgerring
Copy link
Contributor Author

This also touches

* `tests/ui/index_refutable_slice/if_let_slice_binding.stderr`

* `tests/ui/index_refutable_slice/slice_indexing_in_macro.stderr`a

Which are also part of the list in the tracking issue. Can you try to remove the no-rustfix comment there too and if that also Just Work ™️ with your fixes in this PR?

Good catch - These are both good ✅
I've rolled the applicability back down to MaybeIncorrect and removed rust-nofix from these; I think it should be good to go.

@scottgerring scottgerring force-pushed the chore/fix-max-suggested-slice-pattern branch from c07474b to c78a1f4 Compare December 3, 2024 13:21
@scottgerring scottgerring force-pushed the chore/fix-max-suggested-slice-pattern branch from c78a1f4 to e493664 Compare December 3, 2024 14:29
@scottgerring
Copy link
Contributor Author

@flip1995 i've rebased this one onto master and cleaned it up into a single commit. Should be good to go!

Copy link
Member

@y21 y21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, took me a bit to get to this PR. This looks good to me, thank you.

@y21 y21 added this pull request to the merge queue Dec 10, 2024
Merged via the queue into rust-lang:master with commit 2a28347 Dec 10, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants