Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid enabling all EM checks at once (#1349)
- Loading branch information
1 parent
bc9ed0a
commit e290050
Showing
4 changed files
with
56 additions
and
56 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 was deleted.
Oops, something went wrong.
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,4 +1,4 @@ | ||
pub mod checks; | ||
pub mod plugins; | ||
pub mod settings; | ||
|
||
#[cfg(test)] | ||
|
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,52 @@ | ||
use rustpython_ast::{Constant, Expr, ExprKind}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::checks::{Check, CheckCode, CheckKind}; | ||
|
||
/// EM101, EM102, EM103 | ||
pub fn string_in_exception(checker: &mut Checker, exc: &Expr) { | ||
if let ExprKind::Call { args, .. } = &exc.node { | ||
if let Some(first) = args.first() { | ||
match &first.node { | ||
// Check for string literals | ||
ExprKind::Constant { | ||
value: Constant::Str(string), | ||
.. | ||
} => { | ||
if checker.settings.enabled.contains(&CheckCode::EM101) { | ||
if string.len() > checker.settings.flake8_errmsg.max_string_length { | ||
checker.add_check(Check::new( | ||
CheckKind::RawStringInException, | ||
Range::from_located(first), | ||
)); | ||
} | ||
} | ||
} | ||
// Check for f-strings | ||
ExprKind::JoinedStr { .. } => { | ||
if checker.settings.enabled.contains(&CheckCode::EM102) { | ||
checker.add_check(Check::new( | ||
CheckKind::FStringInException, | ||
Range::from_located(first), | ||
)); | ||
} | ||
} | ||
// Check for .format() calls | ||
ExprKind::Call { func, .. } => { | ||
if checker.settings.enabled.contains(&CheckCode::EM103) { | ||
if let ExprKind::Attribute { value, attr, .. } = &func.node { | ||
if attr == "format" && matches!(value.node, ExprKind::Constant { .. }) { | ||
checker.add_check(Check::new( | ||
CheckKind::DotFormatInException, | ||
Range::from_located(first), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
} |