-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Introduce StringLike
enum
#9016
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use ruff_python_ast::StringLike; | ||
|
||
use crate::checkers::ast::Checker; | ||
use crate::codes::Rule; | ||
use crate::rules::{flake8_bandit, flake8_pyi}; | ||
|
||
/// Run lint rules over a [`StringLike`] syntax nodes. | ||
pub(crate) fn string_like(string_like: StringLike, checker: &mut Checker) { | ||
if checker.enabled(Rule::HardcodedBindAllInterfaces) { | ||
flake8_bandit::rules::hardcoded_bind_all_interfaces(checker, string_like); | ||
} | ||
if checker.enabled(Rule::HardcodedTempFile) { | ||
flake8_bandit::rules::hardcoded_tmp_directory(checker, string_like); | ||
} | ||
if checker.source_type.is_stub() { | ||
if checker.enabled(Rule::StringOrBytesTooLong) { | ||
flake8_pyi::rules::string_or_bytes_too_long(checker, string_like); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -393,3 +393,41 @@ impl LiteralExpressionRef<'_> { | |
} | ||
} | ||
} | ||
|
||
/// An enum that holds a reference to a string-like literal from the AST. | ||
/// This includes string literals, bytes literals, and the literal parts of | ||
/// f-strings. | ||
#[derive(Copy, Clone, Debug, PartialEq)] | ||
pub enum StringLike<'a> { | ||
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. Should 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. I don't think so as it also contains bytes literals and implicitly concatenated strings which can possibly do an allocation. |
||
StringLiteral(&'a ast::ExprStringLiteral), | ||
BytesLiteral(&'a ast::ExprBytesLiteral), | ||
FStringLiteral(&'a ast::FStringLiteralElement), | ||
} | ||
|
||
impl<'a> From<&'a ast::ExprStringLiteral> for StringLike<'a> { | ||
fn from(value: &'a ast::ExprStringLiteral) -> Self { | ||
StringLike::StringLiteral(value) | ||
} | ||
} | ||
|
||
impl<'a> From<&'a ast::ExprBytesLiteral> for StringLike<'a> { | ||
fn from(value: &'a ast::ExprBytesLiteral) -> Self { | ||
StringLike::BytesLiteral(value) | ||
} | ||
} | ||
|
||
impl<'a> From<&'a ast::FStringLiteralElement> for StringLike<'a> { | ||
fn from(value: &'a ast::FStringLiteralElement) -> Self { | ||
StringLike::FStringLiteral(value) | ||
} | ||
} | ||
|
||
impl Ranged for StringLike<'_> { | ||
fn range(&self) -> TextRange { | ||
match self { | ||
StringLike::StringLiteral(literal) => literal.range(), | ||
StringLike::BytesLiteral(literal) => literal.range(), | ||
StringLike::FStringLiteral(literal) => literal.range(), | ||
} | ||
} | ||
} |
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.
Maybe just embed this in
analyze::expression
rather than as a separate call here?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.
This might be silly but wouldn't that mix the concerns? I'm fine either way.