Skip to content

Add new ref_string_from_instead_of_str lint - #17838

Open
nicoan wants to merge 1 commit into
rust-lang:masterfrom
nicoan:unnecesary_string_allocation
Open

nicoan wants to merge 1 commit into
rust-lang:masterfrom
nicoan:unnecesary_string_allocation

Conversation

@nicoan

@nicoan nicoan commented Oct 3, 2026 •

Copy link
Copy Markdown

Fixes #3073

changelog: New lint [ref_string_from_instead_of_str]: use &str instead of allocating String.

r? @blyxyas

@rustbot rustbot added needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Oct 3, 2026
@rustbot

This comment has been minimized.

@nicoan
nicoan force-pushed the unnecesary_string_allocation branch from ed4a0e3 to 4a42c4e Compare October 3, 2026 00:14
@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

No changes for 95bcde1

@CommanderStorm CommanderStorm added the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Oct 3, 2026

@CommanderStorm CommanderStorm left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Community review: needs a round of deduplication against the existing lints.
It does not really help our users if we suggest a rewrite twice.. 😉

View changes since this review

/// ```
#[clippy::version = "1.100.0"]
pub REF_STRING_FROM_INSTEAD_OF_STR,
pedantic,

@CommanderStorm CommanderStorm Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

not allocating is perf unless you checked godbold and the code is identical.
Or am I missing something? 🤔

Suggested change
pedantic,
perf,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

No, you are right. I just was not sure what level to put

Comment on lines +67 to +72
_ => (&[], &[], None),
};

let Some(body_owner) = body_owner else {
return;
};

@CommanderStorm CommanderStorm Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

just return here instead of the third being an Option<T>..

Comment on lines +102 to +104
&& args.len() == 1
&& let ExprKind::Lit(ref lit) = args[0].kind
&& let LitKind::Str(s, _) = lit.node

@CommanderStorm CommanderStorm Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as a general rule, please move the structural pattern matching before the queries for linting perf ^^

Also, use structural pattern matching for getting the first arg

}
}

fn get_literal_from_ref_string_from(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<Symbol> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please dedup this and the next function. Feels duplicated xD

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, my intention was to separate the extraction of different patterns, but now that I deduplicated it.. it did not make much sense, thank you

}
}

fn main() {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please make this into an mod main and move everything above inside it

{
let mut s = String::from("hello");
s.push_str(&String::from(" world"));
//~^ ERROR: this creates a needless String allocation, use &str instead

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

instead of the error message, please assert the lint.
Error messages change over time and this is otherwise a bit of a chore ^^

@@ -0,0 +1,97 @@
#![allow(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

consider this.

Suggested change
#![allow(
#![expect(

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

If I use expect, the .fixed file will fail because the lint dissapears:

 --> tests/ui/ref_string_from_instead_of_str.fixed:2:11
  |
2 | #![expect(clippy::needless_borrows_for_generic_args)]
  |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ after rustfix is applied, all errors should be gone, but weren't
  |

full stderr:
error: this lint expectation is unfulfilled
  --> tests/ui/ref_string_from_instead_of_str.fixed:2:11
   |
LL | #![expect(clippy::needless_borrows_for_generic_args)]
   |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `-D unfulfilled-lint-expectations` implied by `-D warnings`
   = help: to override `-D warnings` add `#[allow(unfulfilled_lint_expectations)]`

error: aborting due to 1 previous error


full stdout:


FAILURES:
    tests/ui/ref_string_from_instead_of_str.fixed`

Comment on lines +35 to +39
{
let mut s = String::from("hello");
s.push_str(&" world".to_string());
//~^ ERROR: this creates a needless String allocation, use &str instead
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is already suggested by other lints..

In general if the existing "generic" lints already suggest this rewrite, please ommit the lint from this one.
This applies below too in a bunch of cases

Image

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I added them because I first catalogued the lint as pedantic I was not sure to report this to give extra reasons to the user. Thank you for pointing that those cases should be removed!

@Gri-ffin Gri-ffin 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 Oct 3, 2026
@nicoan
nicoan force-pushed the unnecesary_string_allocation branch from 4a42c4e to 042308b Compare October 5, 2026 19:45
@rustbot

This comment has been minimized.

@CommanderStorm CommanderStorm left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Community review: LGTM.

View changes since this review

@CommanderStorm CommanderStorm left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

^- was meant as an +1

View changes since this review

changelog: New lint [`ref_string_from_instead_of_str`]: use &str instead
of allocating String.
@nicoan
nicoan force-pushed the unnecesary_string_allocation branch from 042308b to 95bcde1 Compare October 6, 2026 13:22
@rustbot

rustbot commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot

rustbot commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

new lint: use string.push_str(&str) instead of string.push_str(&String) for literals

5 participants