-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Add lints for raw string literals #112373
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fb686ff
Add hash count lint for raw string literals
GrishaVar e3ee45b
Fix hash count lint warnings in rustc
GrishaVar 2d9a809
Add lint for unused raw string literals
GrishaVar 559624b
Fix unused raw string lint warnings in rustc
GrishaVar 659f92d
fmt and tidy fixes
GrishaVar 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 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 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -541,6 +541,22 @@ lint_unused_op = unused {$op} that must be used | |||||
.label = the {$op} produces a value | ||||||
.suggestion = use `let _ = ...` to ignore the resulting value | ||||||
|
||||||
lint_unused_raw_string = string literal does not need to be raw. | ||||||
.label = removing the {$contains_hashes -> | ||||||
*[true] `r` and hashes | ||||||
[false] `r` | ||||||
} would result in the same value | ||||||
|
||||||
lint_unused_raw_string_hash = | ||||||
raw string literal uses more hashes than it needs. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise
Suggested change
|
||||||
.label = this raw string requires {$hash_req} {$hash_req -> | ||||||
[one] hash | ||||||
*[other] hashes | ||||||
}, but {$hash_count} {$hash_count -> | ||||||
[one] is | ||||||
*[other] are | ||||||
} used | ||||||
|
||||||
lint_unused_result = unused result of type `{$ty}` | ||||||
|
||||||
lint_variant_size_differences = | ||||||
|
This file contains 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 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 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,122 @@ | ||
use crate::lints::{UnusedRawStringDiag, UnusedRawStringHashDiag}; | ||
use crate::{EarlyContext, EarlyLintPass, LintContext}; | ||
use rustc_ast::{ | ||
token::LitKind::{ByteStrRaw, CStrRaw, StrRaw}, | ||
Expr, ExprKind, | ||
}; | ||
|
||
// Examples / Intuition: | ||
// Must be raw, but hashes are just right r#" " "# (neither warning) | ||
// Must be raw, but has too many hashes r#" \ "# | ||
// Non-raw and has too many hashes r#" ! "# (both warnings) | ||
// Non-raw and hashes are just right r" ! " | ||
|
||
declare_lint! { | ||
/// The `unused_raw_string_hash` lint checks whether raw strings | ||
/// use more hashes than they need. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust,compile_fail | ||
/// #![deny(unused_raw_string_hash)] | ||
/// fn main() { | ||
/// let x = r####"Use the r#"..."# notation for raw strings"####; | ||
/// } | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// The hashes are not needed and should be removed. | ||
pub UNUSED_RAW_STRING_HASH, | ||
Warn, | ||
"Raw string literal has unneeded hashes" | ||
} | ||
|
||
declare_lint_pass!(UnusedRawStringHash => [UNUSED_RAW_STRING_HASH]); | ||
|
||
impl EarlyLintPass for UnusedRawStringHash { | ||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { | ||
if let ExprKind::Lit(lit) = expr.kind { | ||
// Check all raw string variants with one or more hashes | ||
if let StrRaw(hc @ 1..) | ByteStrRaw(hc @ 1..) | CStrRaw(hc @ 1..) = lit.kind { | ||
// Now check if `hash_count` hashes are actually required | ||
let hash_count = hc as usize; | ||
let contents = lit.symbol.as_str(); | ||
let hash_req = Self::required_hashes(contents); | ||
if hash_req < hash_count { | ||
cx.emit_spanned_lint( | ||
UNUSED_RAW_STRING_HASH, | ||
expr.span, | ||
UnusedRawStringHashDiag { span: expr.span, hash_count, hash_req }, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl UnusedRawStringHash { | ||
fn required_hashes(contents: &str) -> usize { | ||
// How many hashes are needed to wrap the input string? | ||
// aka length of longest "#* sequence or zero if none exists | ||
|
||
// FIXME potential speedup: short-circuit max() if `hash_count` found | ||
|
||
contents | ||
.as_bytes() | ||
.split(|&b| b == b'"') | ||
.skip(1) // first element is the only one not starting with " | ||
.map(|bs| 1 + bs.iter().take_while(|&&b| b == b'#').count()) | ||
.max() | ||
.unwrap_or(0) | ||
} | ||
} | ||
|
||
declare_lint! { | ||
/// The `unused_raw_string` lint checks whether raw strings need | ||
/// to be raw. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust,compile_fail | ||
/// #![deny(unused_raw_string)] | ||
/// fn main() { | ||
/// let x = r" totally normal string "; | ||
/// } | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// If a string contains no escapes and no double quotes, it does | ||
/// not need to be raw. | ||
pub UNUSED_RAW_STRING, | ||
Warn, | ||
"String literal does not need to be raw" | ||
} | ||
|
||
declare_lint_pass!(UnusedRawString => [UNUSED_RAW_STRING]); | ||
|
||
impl EarlyLintPass for UnusedRawString { | ||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { | ||
if let ExprKind::Lit(lit) = expr.kind { | ||
// Check all raw string variants | ||
if let StrRaw(hc) | ByteStrRaw(hc) | CStrRaw(hc) = lit.kind { | ||
// Now check if string needs to be raw | ||
let contents = lit.symbol.as_str(); | ||
let contains_hashes = hc > 0; | ||
|
||
if !contents.bytes().any(|b| matches!(b, b'\\' | b'"')) { | ||
cx.emit_spanned_lint( | ||
UNUSED_RAW_STRING, | ||
expr.span, | ||
UnusedRawStringDiag { span: expr.span, contains_hashes }, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the Rust Compiler Development Guide, diagnostics should not have trailing punctuation.