-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
Q004
to AST based checker (#10548)
## Summary Continuing with #7595, this PR moves the `Q004` rule to the AST checker. ## Test Plan - [x] Existing test cases should pass - [x] No ecosystem updates
- Loading branch information
1 parent
d625f55
commit e9115b8
Showing
7 changed files
with
210 additions
and
204 deletions.
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/// Return `true` if the haystack contains an escaped quote. | ||
pub(super) fn contains_escaped_quote(haystack: &str, quote: char) -> bool { | ||
for index in memchr::memchr_iter(quote as u8, haystack.as_bytes()) { | ||
// If the quote is preceded by an even number of backslashes, it's not escaped. | ||
if haystack.as_bytes()[..index] | ||
.iter() | ||
.rev() | ||
.take_while(|&&c| c == b'\\') | ||
.count() | ||
% 2 | ||
!= 0 | ||
{ | ||
return true; | ||
} | ||
} | ||
false | ||
} | ||
|
||
/// Return a modified version of the string with all quote escapes removed. | ||
pub(super) fn unescape_string(haystack: &str, quote: char) -> String { | ||
let mut fixed_contents = String::with_capacity(haystack.len()); | ||
|
||
let mut chars = haystack.chars().peekable(); | ||
let mut backslashes = 0; | ||
while let Some(char) = chars.next() { | ||
if char != '\\' { | ||
fixed_contents.push(char); | ||
backslashes = 0; | ||
continue; | ||
} | ||
// If we're at the end of the line | ||
let Some(next_char) = chars.peek() else { | ||
fixed_contents.push(char); | ||
continue; | ||
}; | ||
// Remove quote escape | ||
if *next_char == quote && backslashes % 2 == 0 { | ||
backslashes = 0; | ||
continue; | ||
} | ||
backslashes += 1; | ||
fixed_contents.push(char); | ||
} | ||
|
||
fixed_contents | ||
} |
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
pub(crate) use avoidable_escaped_quote::*; | ||
pub(crate) use check_string_quotes::*; | ||
pub(crate) use unnecessary_escaped_quote::*; | ||
|
||
mod avoidable_escaped_quote; | ||
mod check_string_quotes; | ||
mod unnecessary_escaped_quote; |
Oops, something went wrong.