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

RUF100 should probably delete an entire comment noqa is part of #12251

Closed
yury-fedotov opened this issue Jul 9, 2024 · 9 comments · Fixed by #13105
Closed

RUF100 should probably delete an entire comment noqa is part of #12251

yury-fedotov opened this issue Jul 9, 2024 · 9 comments · Fixed by #13105
Labels
bug Something isn't working fixes Related to suggested fixes for violations good first issue Good for newcomers help wanted Contributions especially welcome

Comments

@yury-fedotov
Copy link
Contributor

I think it applies to all error codes, but let me give an example where I encountered that.

Consider:

data = do_some_transformations()
return data

Which is a violation of RET504 - unnecessary assignment.

I needed to perform the assignment and return data, not return do_some_transformations(), so added a noqa: RET504 with explanation of why I want to bypass the rule:

data = do_some_transformations()
return data  # noqa: RET504 - to be able to debug the returned object.

Then I disabled RET504 in ruff config completely, and this noqa became unused. Here is where RUF100 comes into play, as it complains on unused error codes, and provides a fix.

The fix was:

-    return data  # noqa: RET504 - to be able to debug the returned object
+    return data  # - to be able to debug the returned object

While I believe it should have been:

-    return data  # noqa: RET504 - to be able to debug the returned object
+    return data

As it's probably quite safe to assume that entire content of the comment noqa is part of is explaining why a rule should by bypassed.

  • List of keywords you searched for before creating this issue: RUF100
  • Most similar open issue: RUF100 for noqa: E501 not at end of line #7851
  • A minimal code snippet that reproduces the bug: see above.
  • The command you invoked: ruff check --fix
  • The current Ruff version: 0.5.1.
@MichaReiser MichaReiser added bug Something isn't working fixes Related to suggested fixes for violations help wanted Contributions especially welcome labels Jul 9, 2024
@charliermarsh
Copy link
Member

I'm unsure -- I think the current behavior is intentional. But either seems reasonable.

@MichaReiser
Copy link
Member

MichaReiser commented Jul 12, 2024

I think deleting the entire comment is fine, for as long as it doesn't delete anything coming after another #:

-    return data  # noqa: RET504 # fmt:skip
+    return data  # fmt: skip

@charliermarsh
Copy link
Member

I agree!

@charliermarsh charliermarsh added the good first issue Good for newcomers label Jul 12, 2024
@charliermarsh
Copy link
Member

I think this is all here if anyone is interested in a good issue:

pub(crate) fn delete_comment(range: TextRange, locator: &Locator) -> Edit {

@jeremeybingham
Copy link

Is there a standard or a strong feeling here on preserving trailing whitespace following 'Preserved' comments? Meaning:

can: x = 1 # noqa # type: ignore
and: x = 1 # noqa # type: ignore

both resolve to: x = 1 # type: ignore ?

or is there a conceivable edge case where that matters?

At any rate, I believe I've mostly got this but will spend some time tomorrow getting the testing framework and everything set up, should work regardless of the above, just changes the logic a bit to save those characters if we want 'em.

@dhruvmanila
Copy link
Member

It should be fine to remove trailing whitespace but just a note that any other comments shouldn't be deleted (#12251 (comment)).

@jeremeybingham
Copy link

this was... much better than I expected the first pass to go, will have a PR on this once I figure out what's going on with these, but I'll take 4/2057 as a start!

Screenshot 2024-07-18 174653

@dhruvmanila
Copy link
Member

@jeremeybingham Can you open the PR? I can help you debug these test failures.

@jeremeybingham
Copy link

will try asap, yes - have "fixed" my way into various fun new failure modes, and am now trying to consolidate the working bits of all that into something that compiles.

dhruvmanila pushed a commit that referenced this issue Aug 29, 2024
## Summary

Extends deletions for RUF100, deleting trailing text from noqa
directives, while preserving upcoming comments on the same line if any.

In cases where it deletes a comment up to another comment on the same
line, the whitespace between them is now shown to be in the autofix in
the diagnostic as well. Leading whitespace before the removed comment is
not, though.

Fixes #12251 

## Test Plan

`cargo test`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations good first issue Good for newcomers help wanted Contributions especially welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants