Replace some Symbol::as_str usage (#14679)

Follow up to https://github.com/rust-lang/rust-clippy/pull/14650

Replaces uses in the form `s.as_str() == "literal"`

r? @y21

changelog: none
This commit is contained in:
Timo 2025-04-27 12:56:14 +00:00 committed by GitHub
commit 0dd9722cdc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
55 changed files with 448 additions and 179 deletions

View file

@ -0,0 +1,21 @@
#![feature(rustc_private)]
extern crate rustc_span;
use clippy_utils::sym;
use rustc_span::{Symbol, kw};
fn f(s: Symbol) {
s == sym::f32;
//~^ symbol_as_str
s == sym::proc_dash_macro;
//~^ symbol_as_str
s == kw::SelfLower;
//~^ symbol_as_str
s == sym::msrv;
//~^ symbol_as_str
s == sym::Cargo_toml;
//~^ symbol_as_str
sym::get == s;
//~^ symbol_as_str
}

View file

@ -0,0 +1,21 @@
#![feature(rustc_private)]
extern crate rustc_span;
use clippy_utils::sym;
use rustc_span::{Symbol, kw};
fn f(s: Symbol) {
s.as_str() == "f32";
//~^ symbol_as_str
s.as_str() == "proc-macro";
//~^ symbol_as_str
s.as_str() == "self";
//~^ symbol_as_str
s.as_str() == "msrv";
//~^ symbol_as_str
s.as_str() == "Cargo.toml";
//~^ symbol_as_str
"get" == s.as_str();
//~^ symbol_as_str
}

View file

@ -0,0 +1,76 @@
error: converting a Symbol to a string
--> tests/ui-internal/symbol_as_str.rs:9:5
|
LL | s.as_str() == "f32";
| ^^^^^^^^^^
|
= note: `-D clippy::symbol-as-str` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::symbol_as_str)]`
help: use the preinterned symbol
|
LL - s.as_str() == "f32";
LL + s == sym::f32;
|
error: converting a Symbol to a string
--> tests/ui-internal/symbol_as_str.rs:11:5
|
LL | s.as_str() == "proc-macro";
| ^^^^^^^^^^
|
help: use the preinterned symbol
|
LL - s.as_str() == "proc-macro";
LL + s == sym::proc_dash_macro;
|
error: converting a Symbol to a string
--> tests/ui-internal/symbol_as_str.rs:13:5
|
LL | s.as_str() == "self";
| ^^^^^^^^^^
|
help: use the preinterned symbol
|
LL - s.as_str() == "self";
LL + s == kw::SelfLower;
|
error: converting a Symbol to a string
--> tests/ui-internal/symbol_as_str.rs:15:5
|
LL | s.as_str() == "msrv";
| ^^^^^^^^^^
|
help: use the preinterned symbol
|
LL - s.as_str() == "msrv";
LL + s == sym::msrv;
|
error: converting a Symbol to a string
--> tests/ui-internal/symbol_as_str.rs:17:5
|
LL | s.as_str() == "Cargo.toml";
| ^^^^^^^^^^
|
help: use the preinterned symbol
|
LL - s.as_str() == "Cargo.toml";
LL + s == sym::Cargo_toml;
|
error: converting a Symbol to a string
--> tests/ui-internal/symbol_as_str.rs:19:14
|
LL | "get" == s.as_str();
| ^^^^^^^^^^
|
help: use the preinterned symbol
|
LL - "get" == s.as_str();
LL + sym::get == s;
|
error: aborting due to 6 previous errors

View file

@ -0,0 +1,15 @@
//@no-rustfix: paths that don't exist yet
#![feature(rustc_private)]
extern crate rustc_span;
use rustc_span::Symbol;
fn f(s: Symbol) {
s.as_str() == "xyz123";
//~^ symbol_as_str
s.as_str() == "with-dash";
//~^ symbol_as_str
s.as_str() == "with.dot";
//~^ symbol_as_str
}

View file

@ -0,0 +1,40 @@
error: converting a Symbol to a string
--> tests/ui-internal/symbol_as_str_unfixable.rs:9:5
|
LL | s.as_str() == "xyz123";
| ^^^^^^^^^^
|
= note: `-D clippy::symbol-as-str` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::symbol_as_str)]`
help: add the symbol to `clippy_utils/src/sym.rs` and use it
|
LL - s.as_str() == "xyz123";
LL + s == sym::xyz123;
|
error: converting a Symbol to a string
--> tests/ui-internal/symbol_as_str_unfixable.rs:11:5
|
LL | s.as_str() == "with-dash";
| ^^^^^^^^^^
|
help: add the symbol to `clippy_utils/src/sym.rs` and use it
|
LL - s.as_str() == "with-dash";
LL + s == sym::with_dash;
|
error: converting a Symbol to a string
--> tests/ui-internal/symbol_as_str_unfixable.rs:13:5
|
LL | s.as_str() == "with.dot";
| ^^^^^^^^^^
|
help: add the symbol to `clippy_utils/src/sym.rs` and use it
|
LL - s.as_str() == "with.dot";
LL + s == sym::with_dot;
|
error: aborting due to 3 previous errors