-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New lint [needless_raw_string_hashes]
#10884
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1bf74fc
add `needless_raw_string_hashes` lint
Centri3 bc744eb
new lint `needless_raw_string` + refactor a bit
Centri3 ec765d9
Update raw_strings.rs
Centri3 cb52d19
don't lint `needless_raw_string_hashes` when it's unnecessary
Centri3 8cb6c86
change category and refactor
Centri3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| use std::{iter::once, ops::ControlFlow}; | ||
|
|
||
| use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet}; | ||
| use rustc_ast::{ | ||
| ast::{Expr, ExprKind}, | ||
| token::LitKind, | ||
| }; | ||
| use rustc_errors::Applicability; | ||
| use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; | ||
| use rustc_middle::lint::in_external_macro; | ||
| use rustc_session::{declare_tool_lint, impl_lint_pass}; | ||
|
|
||
| declare_clippy_lint! { | ||
| /// ### What it does | ||
| /// Checks for raw string literals where a string literal can be used instead. | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// It's just unnecessary, but there are many cases where using a raw string literal is more | ||
| /// idiomatic than a string literal, so it's opt-in. | ||
| /// | ||
| /// ### Example | ||
| /// ```rust | ||
| /// let r = r"Hello, world!"; | ||
| /// ``` | ||
| /// Use instead: | ||
| /// ```rust | ||
| /// let r = "Hello, world!"; | ||
| /// ``` | ||
| #[clippy::version = "1.72.0"] | ||
| pub NEEDLESS_RAW_STRINGS, | ||
| restriction, | ||
| "suggests using a string literal when a raw string literal is unnecessary" | ||
| } | ||
| declare_clippy_lint! { | ||
| /// ### What it does | ||
| /// Checks for raw string literals with an unnecessary amount of hashes around them. | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// It's just unnecessary, and makes it look like there's more escaping needed than is actually | ||
| /// necessary. | ||
| /// | ||
| /// ### Example | ||
| /// ```rust | ||
| /// let r = r###"Hello, "world"!"###; | ||
| /// ``` | ||
| /// Use instead: | ||
| /// ```rust | ||
| /// let r = r#"Hello, "world"!"#; | ||
| /// ``` | ||
| #[clippy::version = "1.72.0"] | ||
| pub NEEDLESS_RAW_STRING_HASHES, | ||
| style, | ||
| "suggests reducing the number of hashes around a raw string literal" | ||
| } | ||
| impl_lint_pass!(RawStrings => [NEEDLESS_RAW_STRINGS, NEEDLESS_RAW_STRING_HASHES]); | ||
|
|
||
| pub struct RawStrings { | ||
| pub needless_raw_string_hashes_allow_one: bool, | ||
| } | ||
|
|
||
| impl EarlyLintPass for RawStrings { | ||
| fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { | ||
| if !in_external_macro(cx.sess(), expr.span) | ||
| && let ExprKind::Lit(lit) = expr.kind | ||
| && let LitKind::StrRaw(max) | LitKind::ByteStrRaw(max) | LitKind::CStrRaw(max) = lit.kind | ||
| { | ||
| let str = lit.symbol.as_str(); | ||
| let prefix = match lit.kind { | ||
| LitKind::StrRaw(..) => "r", | ||
| LitKind::ByteStrRaw(..) => "br", | ||
| LitKind::CStrRaw(..) => "cr", | ||
| _ => unreachable!(), | ||
| }; | ||
| if !snippet(cx, expr.span, prefix).trim().starts_with(prefix) { | ||
| return; | ||
| } | ||
|
|
||
| if !str.contains(['\\', '"']) { | ||
| span_lint_and_sugg( | ||
| cx, | ||
| NEEDLESS_RAW_STRINGS, | ||
| expr.span, | ||
| "unnecessary raw string literal", | ||
| "try", | ||
| format!("{}\"{}\"", prefix.replace('r', ""), lit.symbol), | ||
| Applicability::MachineApplicable, | ||
| ); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| let req = { | ||
| let mut following_quote = false; | ||
| let mut req = 0; | ||
| // `once` so a raw string ending in hashes is still checked | ||
| let num = str.as_bytes().iter().chain(once(&0)).try_fold(0u8, |acc, &b| { | ||
| match b { | ||
| b'"' => (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), | ||
| _ => { | ||
| if following_quote { | ||
| following_quote = false; | ||
|
|
||
| if req == max { | ||
| return ControlFlow::Break(req); | ||
| } | ||
|
|
||
| return ControlFlow::Continue(acc.max(req)); | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| ControlFlow::Continue(acc) | ||
| }); | ||
|
|
||
| match num { | ||
| ControlFlow::Continue(num) | ControlFlow::Break(num) => num, | ||
| } | ||
| }; | ||
|
|
||
| if req < max { | ||
| let hashes = "#".repeat(req as usize); | ||
|
|
||
| span_lint_and_sugg( | ||
| cx, | ||
| NEEDLESS_RAW_STRING_HASHES, | ||
| expr.span, | ||
| "unnecessary hashes around raw string literal", | ||
| "try", | ||
| format!(r#"{prefix}{hashes}"{}"{hashes}"#, lit.symbol), | ||
| Applicability::MachineApplicable, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.