Auto merge of #11416 - Alexendoo:raw-strings-multipart, r=xFrednet

Use multipart suggestions for raw string lints

Should make it slightly easier to see the suggested edit

Before/after for `needless_raw_string_hashes`:

| Before| After |
|--------|--------|
| ![before](https://github.com/rust-lang/rust-clippy/assets/1830331/da52a436-d890-4594-9191-819c1af946c7) | ![after](https://github.com/rust-lang/rust-clippy/assets/1830331/9731d790-8efa-42a2-b2e9-0ec51398f8f3) |

changelog: none
This commit is contained in:
bors 2023-09-01 22:19:57 +00:00
commit a8b5245ea3
8 changed files with 289 additions and 52 deletions

View file

@ -1,7 +1,7 @@
use std::iter::once; use std::iter::once;
use std::ops::ControlFlow; use std::ops::ControlFlow;
use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet; use clippy_utils::source::snippet;
use rustc_ast::ast::{Expr, ExprKind}; use rustc_ast::ast::{Expr, ExprKind};
use rustc_ast::token::LitKind; use rustc_ast::token::LitKind;
@ -9,6 +9,7 @@ use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::{BytePos, Pos, Span};
declare_clippy_lint! { declare_clippy_lint! {
/// ### What it does /// ### What it does
@ -76,14 +77,24 @@ impl EarlyLintPass for RawStrings {
} }
if !str.contains(['\\', '"']) { if !str.contains(['\\', '"']) {
span_lint_and_sugg( span_lint_and_then(
cx, cx,
NEEDLESS_RAW_STRINGS, NEEDLESS_RAW_STRINGS,
expr.span, expr.span,
"unnecessary raw string literal", "unnecessary raw string literal",
"try", |diag| {
format!("{}\"{}\"", prefix.replace('r', ""), lit.symbol), let (start, end) = hash_spans(expr.span, prefix, 0, max);
Applicability::MachineApplicable,
// BytePos: skip over the `b` in `br`, we checked the prefix appears in the source text
let r_pos = expr.span.lo() + BytePos::from_usize(prefix.len() - 1);
let start = start.with_lo(r_pos);
diag.multipart_suggestion(
"try",
vec![(start, String::new()), (end, String::new())],
Applicability::MachineApplicable,
);
},
); );
return; return;
@ -96,13 +107,6 @@ impl EarlyLintPass for RawStrings {
let num = str.as_bytes().iter().chain(once(&0)).try_fold(0u8, |acc, &b| { let num = str.as_bytes().iter().chain(once(&0)).try_fold(0u8, |acc, &b| {
match b { match b {
b'"' if !following_quote => (following_quote, req) = (true, 1), b'"' if !following_quote => (following_quote, req) = (true, 1),
// I'm a bit surprised the compiler didn't optimize this out, there's no
// branch but it still ends up doing an unnecessary comparison, it's:
// - cmp r9b,1h
// - sbb cl,-1h
// which will add 1 if it's true. With this change, it becomes:
// - add cl,r9b
// isn't that so much nicer?
b'#' => req += u8::from(following_quote), b'#' => req += u8::from(following_quote),
_ => { _ => {
if following_quote { if following_quote {
@ -126,18 +130,58 @@ impl EarlyLintPass for RawStrings {
}; };
if req < max { if req < max {
let hashes = "#".repeat(req as usize); span_lint_and_then(
span_lint_and_sugg(
cx, cx,
NEEDLESS_RAW_STRING_HASHES, NEEDLESS_RAW_STRING_HASHES,
expr.span, expr.span,
"unnecessary hashes around raw string literal", "unnecessary hashes around raw string literal",
"try", |diag| {
format!(r#"{prefix}{hashes}"{}"{hashes}"#, lit.symbol), let (start, end) = hash_spans(expr.span, prefix, req, max);
Applicability::MachineApplicable,
let message = match max - req {
_ if req == 0 => "remove all the hashes around the literal".to_string(),
1 => "remove one hash from both sides of the literal".to_string(),
n => format!("remove {n} hashes from both sides of the literal"),
};
diag.multipart_suggestion(
message,
vec![(start, String::new()), (end, String::new())],
Applicability::MachineApplicable,
);
},
); );
} }
} }
} }
} }
/// Returns spans pointing at the unneeded hashes, e.g. for a `req` of `1` and `max` of `3`:
///
/// ```ignore
/// r###".."###
/// ^^ ^^
/// ```
fn hash_spans(literal_span: Span, prefix: &str, req: u8, max: u8) -> (Span, Span) {
let literal_span = literal_span.data();
// BytePos: we checked prefix appears literally in the source text
let hash_start = literal_span.lo + BytePos::from_usize(prefix.len());
let hash_end = literal_span.hi;
// BytePos: req/max are counts of the ASCII character #
let start = Span::new(
hash_start + BytePos(req.into()),
hash_start + BytePos(max.into()),
literal_span.ctxt,
None,
);
let end = Span::new(
hash_end - BytePos(req.into()),
hash_end - BytePos(max.into()),
literal_span.ctxt,
None,
);
(start, end)
}

View file

@ -9,8 +9,13 @@ fn main() {
b"aaa"; b"aaa";
br#""aaa""#; br#""aaa""#;
br#"\s"#; br#"\s"#;
// currently disabled: https://github.com/rust-lang/rust/issues/113333 c"aaa";
// cr#"aaa"#; cr#""aaa""#;
// cr#""aaa""#; cr#"\s"#;
// cr#"\s"#;
"
a
multiline
string
";
} }

View file

@ -9,8 +9,13 @@ fn main() {
br#"aaa"#; br#"aaa"#;
br#""aaa""#; br#""aaa""#;
br#"\s"#; br#"\s"#;
// currently disabled: https://github.com/rust-lang/rust/issues/113333 cr#"aaa"#;
// cr#"aaa"#; cr#""aaa""#;
// cr#""aaa""#; cr#"\s"#;
// cr#"\s"#;
r#"
a
multiline
string
"#;
} }

View file

@ -2,15 +2,57 @@ error: unnecessary raw string literal
--> $DIR/needless_raw_string.rs:6:5 --> $DIR/needless_raw_string.rs:6:5
| |
LL | r#"aaa"#; LL | r#"aaa"#;
| ^^^^^^^^ help: try: `"aaa"` | ^^^^^^^^
| |
= note: `-D clippy::needless-raw-strings` implied by `-D warnings` = note: `-D clippy::needless-raw-strings` implied by `-D warnings`
help: try
|
LL - r#"aaa"#;
LL + "aaa";
|
error: unnecessary raw string literal error: unnecessary raw string literal
--> $DIR/needless_raw_string.rs:9:5 --> $DIR/needless_raw_string.rs:9:5
| |
LL | br#"aaa"#; LL | br#"aaa"#;
| ^^^^^^^^^ help: try: `b"aaa"` | ^^^^^^^^^
|
help: try
|
LL - br#"aaa"#;
LL + b"aaa";
|
error: aborting due to 2 previous errors error: unnecessary raw string literal
--> $DIR/needless_raw_string.rs:12:5
|
LL | cr#"aaa"#;
| ^^^^^^^^^
|
help: try
|
LL - cr#"aaa"#;
LL + c"aaa";
|
error: unnecessary raw string literal
--> $DIR/needless_raw_string.rs:16:5
|
LL | / r#"
LL | | a
LL | | multiline
LL | | string
LL | | "#;
| |______^
|
help: try
|
LL ~ "
LL | a
LL | multiline
LL | string
LL ~ ";
|
error: aborting due to 4 previous errors

View file

@ -3,17 +3,22 @@
#![feature(c_str_literals)] #![feature(c_str_literals)]
fn main() { fn main() {
r#"aaa"#; r"\aaa";
r#"Hello "world"!"#; r#"Hello "world"!"#;
r####" "### "## "# "####; r####" "### "## "# "####;
r###" "aa" "# "## "###; r###" "aa" "# "## "###;
br#"aaa"#; br"\aaa";
br#"Hello "world"!"#; br#"Hello "world"!"#;
br####" "### "## "# "####; br####" "### "## "# "####;
br###" "aa" "# "## "###; br###" "aa" "# "## "###;
// currently disabled: https://github.com/rust-lang/rust/issues/113333 cr"\aaa";
// cr#"aaa"#; cr#"Hello "world"!"#;
// cr##"Hello "world"!"##; cr####" "### "## "# "####;
// cr######" "### "## "# "######; cr###" "aa" "# "## "###;
// cr######" "aa" "# "## "######;
r"
\a
multiline
string
";
} }

View file

@ -3,17 +3,22 @@
#![feature(c_str_literals)] #![feature(c_str_literals)]
fn main() { fn main() {
r#"aaa"#; r#"\aaa"#;
r##"Hello "world"!"##; r##"Hello "world"!"##;
r######" "### "## "# "######; r######" "### "## "# "######;
r######" "aa" "# "## "######; r######" "aa" "# "## "######;
br#"aaa"#; br#"\aaa"#;
br##"Hello "world"!"##; br##"Hello "world"!"##;
br######" "### "## "# "######; br######" "### "## "# "######;
br######" "aa" "# "## "######; br######" "aa" "# "## "######;
// currently disabled: https://github.com/rust-lang/rust/issues/113333 cr#"\aaa"#;
// cr#"aaa"#; cr##"Hello "world"!"##;
// cr##"Hello "world"!"##; cr######" "### "## "# "######;
// cr######" "### "## "# "######; cr######" "aa" "# "## "######;
// cr######" "aa" "# "## "######;
r#"
\a
multiline
string
"#;
} }

View file

@ -1,40 +1,166 @@
error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:6:5
|
LL | r#"\aaa"#;
| ^^^^^^^^^
|
= note: `-D clippy::needless-raw-string-hashes` implied by `-D warnings`
help: remove all the hashes around the literal
|
LL - r#"\aaa"#;
LL + r"\aaa";
|
error: unnecessary hashes around raw string literal error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:7:5 --> $DIR/needless_raw_string_hashes.rs:7:5
| |
LL | r##"Hello "world"!"##; LL | r##"Hello "world"!"##;
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `r#"Hello "world"!"#` | ^^^^^^^^^^^^^^^^^^^^^
|
help: remove one hash from both sides of the literal
|
LL - r##"Hello "world"!"##;
LL + r#"Hello "world"!"#;
| |
= note: `-D clippy::needless-raw-string-hashes` implied by `-D warnings`
error: unnecessary hashes around raw string literal error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:8:5 --> $DIR/needless_raw_string_hashes.rs:8:5
| |
LL | r######" "### "## "# "######; LL | r######" "### "## "# "######;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `r####" "### "## "# "####` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove 2 hashes from both sides of the literal
|
LL - r######" "### "## "# "######;
LL + r####" "### "## "# "####;
|
error: unnecessary hashes around raw string literal error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:9:5 --> $DIR/needless_raw_string_hashes.rs:9:5
| |
LL | r######" "aa" "# "## "######; LL | r######" "aa" "# "## "######;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `r###" "aa" "# "## "###` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove 3 hashes from both sides of the literal
|
LL - r######" "aa" "# "## "######;
LL + r###" "aa" "# "## "###;
|
error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:10:5
|
LL | br#"\aaa"#;
| ^^^^^^^^^^
|
help: remove all the hashes around the literal
|
LL - br#"\aaa"#;
LL + br"\aaa";
|
error: unnecessary hashes around raw string literal error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:11:5 --> $DIR/needless_raw_string_hashes.rs:11:5
| |
LL | br##"Hello "world"!"##; LL | br##"Hello "world"!"##;
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `br#"Hello "world"!"#` | ^^^^^^^^^^^^^^^^^^^^^^
|
help: remove one hash from both sides of the literal
|
LL - br##"Hello "world"!"##;
LL + br#"Hello "world"!"#;
|
error: unnecessary hashes around raw string literal error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:12:5 --> $DIR/needless_raw_string_hashes.rs:12:5
| |
LL | br######" "### "## "# "######; LL | br######" "### "## "# "######;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `br####" "### "## "# "####` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove 2 hashes from both sides of the literal
|
LL - br######" "### "## "# "######;
LL + br####" "### "## "# "####;
|
error: unnecessary hashes around raw string literal error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:13:5 --> $DIR/needless_raw_string_hashes.rs:13:5
| |
LL | br######" "aa" "# "## "######; LL | br######" "aa" "# "## "######;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `br###" "aa" "# "## "###` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove 3 hashes from both sides of the literal
|
LL - br######" "aa" "# "## "######;
LL + br###" "aa" "# "## "###;
|
error: aborting due to 6 previous errors error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:14:5
|
LL | cr#"\aaa"#;
| ^^^^^^^^^^
|
help: remove all the hashes around the literal
|
LL - cr#"\aaa"#;
LL + cr"\aaa";
|
error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:15:5
|
LL | cr##"Hello "world"!"##;
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: remove one hash from both sides of the literal
|
LL - cr##"Hello "world"!"##;
LL + cr#"Hello "world"!"#;
|
error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:16:5
|
LL | cr######" "### "## "# "######;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove 2 hashes from both sides of the literal
|
LL - cr######" "### "## "# "######;
LL + cr####" "### "## "# "####;
|
error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:17:5
|
LL | cr######" "aa" "# "## "######;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove 3 hashes from both sides of the literal
|
LL - cr######" "aa" "# "## "######;
LL + cr###" "aa" "# "## "###;
|
error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:19:5
|
LL | / r#"
LL | | \a
LL | | multiline
LL | | string
LL | | "#;
| |______^
|
help: remove all the hashes around the literal
|
LL ~ r"
LL | \a
LL | multiline
LL | string
LL ~ ";
|
error: aborting due to 13 previous errors

View file

@ -2,9 +2,14 @@ error: unnecessary raw string literal
--> $DIR/write_literal_2.rs:13:24 --> $DIR/write_literal_2.rs:13:24
| |
LL | writeln!(v, r"{}", r"{hello}"); LL | writeln!(v, r"{}", r"{hello}");
| ^^^^^^^^^^ help: try: `"{hello}"` | ^^^^^^^^^^
| |
= note: `-D clippy::needless-raw-strings` implied by `-D warnings` = note: `-D clippy::needless-raw-strings` implied by `-D warnings`
help: try
|
LL - writeln!(v, r"{}", r"{hello}");
LL + writeln!(v, r"{}", "{hello}");
|
error: literal with an empty format string error: literal with an empty format string
--> $DIR/write_literal_2.rs:10:23 --> $DIR/write_literal_2.rs:10:23