Skip to content

[methods] Move lints from strings.rs to methods module (#14253) #14362

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

paolo9921
Copy link


changelog: [refactor-strings-lints]: Move lints (except for: string_add, string_add_assign and string_slice (which share an implementation) from clippy_lints/src/strings.rs to the methods module to share tree traversal with other method and function call lints. Fixes #14253.

fixes #14253

@rustbot
Copy link
Collaborator

rustbot commented Mar 6, 2025

r? @dswij

rustbot has assigned @dswij.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 6, 2025
@paolo9921
Copy link
Author

Hi, I'm getting this error:
error: callingas_bytes()` on a string literal

  • --> tests/ui/string_lit_as_bytes.rs:11:26
  • |
    -LL | const B: &[u8] = $b.as_bytes();
  • | ^^^^^^^^^^^^^ help: consider using a byte string literal instead: b"warning"
    -...
    -LL | b!("warning");
  • | ------------- in this macro invocation
  • |
  • = note: this error originates in the macro b (in Nightly builds, run with -Z macro-backtrace for more info)

-error: calling as_bytes() on include_str!(..)

  • --> tests/ui/string_lit_as_bytes.rs:43:22
  • |
    -LL | let includestr = include_str!("string_lit_as_bytes.rs").as_bytes();
  • | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using include_bytes!(..) instead: include_bytes!("string_lit_as_bytes.rs")

-error: calling as_bytes() on a string literal

error: diagnostic code clippy::string_lit_as_bytes not found on line 11
--> tests/ui/string_lit_as_bytes.rs:12:14
|
12 | //~^ string_lit_as_bytes
| ^^^^^^^^^^^^^^^^^^^ expected because of this pattern
|

error: diagnostic code clippy::string_lit_as_bytes not found on line 43
--> tests/ui/string_lit_as_bytes.rs:44:10
|
44 | //~^ string_lit_as_bytes
| ^^^^^^^^^^^^^^^^^^^ expected because of this pattern
`

I can't understand.. I have to change the code of the test?

@samueltardieu
Copy link
Member

Thanks for the refactoring.

The problem here you are encountering here is twofold:

  • Methods::check() calls Methods::check_methods() only if the method call does not come from expanded code.
  • methods::check_methods() uses methods::method_call() which returns None if either the method receiver or any of the method argument comes expanded code.

This is done so because to keep the method checking lints simple and less sensitive to subtle bugs, it is simpler to avoid dealing with expanded code. However, in your case, the string_lint_as_bytes lint is supposed to trigger in the presence of a macro as the receiver, or inside a macro.

One possibility would be to apply a commit such as samueltardieu@2e77605 on top of your commit: this introduces the possibility to run some methods lint even in the presence of expansion. Those lints are then in charge of properly checking their context and make sure they behave properly.

let ty = cx.typeck_results().expr_ty(receiver);

// &str -> String
if let ty::Ref(_, ty, ..) = ty.kind() { // Usa ty::Ref invece di rustc_middle::ty::TyKind::Ref
Copy link
Member

Choose a reason for hiding this comment

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

Comments in Clippy source code are in English.

expr: &'tcx Expr<'_>,
method_name: &str,
receiver: &'tcx Expr<'_>,
_args: &[Expr<'_>],
Copy link
Member

Choose a reason for hiding this comment

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

You should simply remove unused args.

@paolo9921 paolo9921 marked this pull request as draft March 7, 2025 15:23
@rustbot
Copy link
Collaborator

rustbot commented Mar 31, 2025

☔ The latest upstream changes (possibly d28d234) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Mar 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor the clippy_lints/src/strings.rs file
4 participants