Disallow rule names ending in *-used

This commit is contained in:
Martin Fischer 2023-02-10 06:26:29 +01:00 committed by Charlie Marsh
parent bfbde537af
commit 1cbe48522e
14 changed files with 40 additions and 39 deletions

View file

@ -664,7 +664,7 @@ For more, see [Pyflakes](https://pypi.org/project/pyflakes/) on PyPI.
| ---- | ---- | ------- | --- |
| F401 | unused-import | `{name}` imported but unused; consider adding to `__all__` or using a redundant alias | 🛠 |
| F402 | import-shadowed-by-loop-var | Import `{name}` from line {line} shadowed by loop variable | |
| F403 | import-star-used | `from {name} import *` used; unable to detect undefined names | |
| F403 | import-star | `from {name} import *` used; unable to detect undefined names | |
| F404 | late-future-import | `from __future__` imports must occur at the beginning of the file | |
| F405 | import-star-usage | `{name}` may be undefined, or defined from star imports: {sources} | |
| F406 | import-star-not-permitted | `from {name} import *` only allowed at module level | |
@ -918,8 +918,8 @@ For more, see [flake8-bandit](https://pypi.org/project/flake8-bandit/) on PyPI.
| Code | Name | Message | Fix |
| ---- | ---- | ------- | --- |
| S101 | assert-used | Use of `assert` detected | |
| S102 | exec-used | Use of `exec` detected | |
| S101 | assert | Use of `assert` detected | |
| S102 | exec-builtin | Use of `exec` detected | |
| S103 | bad-file-permissions | `os.chmod` setting a permissive mask `{mask:#o}` on file or directory | |
| S104 | hardcoded-bind-all-interfaces | Possible binding to all interfaces | |
| S105 | hardcoded-password-string | Possible hardcoded password: "{}" | |

View file

@ -1 +1,2 @@
do-not-*
*-used

View file

@ -1226,9 +1226,9 @@ where
}
}
if self.settings.rules.enabled(&Rule::ImportStarUsed) {
if self.settings.rules.enabled(&Rule::ImportStar) {
self.diagnostics.push(Diagnostic::new(
pyflakes::rules::ImportStarUsed {
pyflakes::rules::ImportStar {
name: helpers::format_import_from(
level.as_ref(),
module.as_deref(),
@ -1536,7 +1536,7 @@ where
msg.as_ref().map(|expr| &**expr),
);
}
if self.settings.rules.enabled(&Rule::AssertUsed) {
if self.settings.rules.enabled(&Rule::Assert) {
self.diagnostics
.push(flake8_bandit::rules::assert_used(stmt));
}
@ -2369,7 +2369,7 @@ where
}
// flake8-bandit
if self.settings.rules.enabled(&Rule::ExecUsed) {
if self.settings.rules.enabled(&Rule::ExecBuiltin) {
if let Some(diagnostic) = flake8_bandit::rules::exec_used(expr, func) {
self.diagnostics.push(diagnostic);
}

View file

@ -83,7 +83,7 @@ ruff_macros::define_rule_mapping!(
// pyflakes
F401 => rules::pyflakes::rules::UnusedImport,
F402 => rules::pyflakes::rules::ImportShadowedByLoopVar,
F403 => rules::pyflakes::rules::ImportStarUsed,
F403 => rules::pyflakes::rules::ImportStar,
F404 => rules::pyflakes::rules::LateFutureImport,
F405 => rules::pyflakes::rules::ImportStarUsage,
F406 => rules::pyflakes::rules::ImportStarNotPermitted,
@ -381,8 +381,8 @@ ruff_macros::define_rule_mapping!(
// eradicate
ERA001 => rules::eradicate::rules::CommentedOutCode,
// flake8-bandit
S101 => rules::flake8_bandit::rules::AssertUsed,
S102 => rules::flake8_bandit::rules::ExecUsed,
S101 => rules::flake8_bandit::rules::Assert,
S102 => rules::flake8_bandit::rules::ExecBuiltin,
S103 => rules::flake8_bandit::rules::BadFilePermissions,
S104 => rules::flake8_bandit::rules::HardcodedBindAllInterfaces,
S105 => rules::flake8_bandit::rules::HardcodedPasswordString,

View file

@ -15,8 +15,8 @@ mod tests {
use crate::settings::Settings;
use crate::test::test_path;
#[test_case(Rule::AssertUsed, Path::new("S101.py"); "S101")]
#[test_case(Rule::ExecUsed, Path::new("S102.py"); "S102")]
#[test_case(Rule::Assert, Path::new("S101.py"); "S101")]
#[test_case(Rule::ExecBuiltin, Path::new("S102.py"); "S102")]
#[test_case(Rule::BadFilePermissions, Path::new("S103.py"); "S103")]
#[test_case(Rule::HardcodedBindAllInterfaces, Path::new("S104.py"); "S104")]
#[test_case(Rule::HardcodedPasswordString, Path::new("S105.py"); "S105")]

View file

@ -6,9 +6,9 @@ use crate::registry::Diagnostic;
use crate::violation::Violation;
define_violation!(
pub struct AssertUsed;
pub struct Assert;
);
impl Violation for AssertUsed {
impl Violation for Assert {
#[derive_message_formats]
fn message(&self) -> String {
format!("Use of `assert` detected")
@ -18,7 +18,7 @@ impl Violation for AssertUsed {
/// S101
pub fn assert_used(stmt: &Located<StmtKind>) -> Diagnostic {
Diagnostic::new(
AssertUsed,
Assert,
Range::new(stmt.location, stmt.location.with_col_offset("assert".len())),
)
}

View file

@ -6,9 +6,9 @@ use crate::registry::Diagnostic;
use crate::violation::Violation;
define_violation!(
pub struct ExecUsed;
pub struct ExecBuiltin;
);
impl Violation for ExecUsed {
impl Violation for ExecBuiltin {
#[derive_message_formats]
fn message(&self) -> String {
format!("Use of `exec` detected")
@ -23,5 +23,5 @@ pub fn exec_used(expr: &Expr, func: &Expr) -> Option<Diagnostic> {
if id != "exec" {
return None;
}
Some(Diagnostic::new(ExecUsed, Range::from_located(expr)))
Some(Diagnostic::new(ExecBuiltin, Range::from_located(expr)))
}

View file

@ -1,6 +1,6 @@
pub use assert_used::{assert_used, AssertUsed};
pub use assert_used::{assert_used, Assert};
pub use bad_file_permissions::{bad_file_permissions, BadFilePermissions};
pub use exec_used::{exec_used, ExecUsed};
pub use exec_used::{exec_used, ExecBuiltin};
pub use hardcoded_bind_all_interfaces::{
hardcoded_bind_all_interfaces, HardcodedBindAllInterfaces,
};

View file

@ -1,9 +1,9 @@
---
source: src/rules/flake8_bandit/mod.rs
source: crates/ruff/src/rules/flake8_bandit/mod.rs
expression: diagnostics
---
- kind:
AssertUsed: ~
Assert: ~
location:
row: 2
column: 0
@ -13,7 +13,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
AssertUsed: ~
Assert: ~
location:
row: 8
column: 4
@ -23,7 +23,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
AssertUsed: ~
Assert: ~
location:
row: 11
column: 4

View file

@ -1,9 +1,9 @@
---
source: src/rules/flake8_bandit/mod.rs
source: crates/ruff/src/rules/flake8_bandit/mod.rs
expression: diagnostics
---
- kind:
ExecUsed: ~
ExecBuiltin: ~
location:
row: 3
column: 4
@ -13,7 +13,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ExecUsed: ~
ExecBuiltin: ~
location:
row: 5
column: 0

View file

@ -31,7 +31,7 @@ mod tests {
#[test_case(Rule::UnusedImport, Path::new("F401_7.py"); "F401_7")]
#[test_case(Rule::UnusedImport, Path::new("F401_8.py"); "F401_8")]
#[test_case(Rule::ImportShadowedByLoopVar, Path::new("F402.py"); "F402")]
#[test_case(Rule::ImportStarUsed, Path::new("F403.py"); "F403")]
#[test_case(Rule::ImportStar, Path::new("F403.py"); "F403")]
#[test_case(Rule::LateFutureImport, Path::new("F404.py"); "F404")]
#[test_case(Rule::ImportStarUsage, Path::new("F405.py"); "F405")]
#[test_case(Rule::ImportStarNotPermitted, Path::new("F406.py"); "F406")]
@ -460,7 +460,7 @@ mod tests {
// Can't find undefined names with import *.
flakes(
"from fu import *; bar",
&[Rule::ImportStarUsed, Rule::ImportStarUsage],
&[Rule::ImportStar, Rule::ImportStarUsage],
);
}
@ -2476,7 +2476,7 @@ mod tests {
csc(1)
"#,
&[
Rule::ImportStarUsed,
Rule::ImportStar,
Rule::ImportStarUsage,
Rule::ImportStarUsage,
Rule::ImportStarUsage,
@ -2494,7 +2494,7 @@ mod tests {
a = 1
__all__ = ['a']
"#,
&[Rule::ImportStarUsed, Rule::UnusedImport],
&[Rule::ImportStar, Rule::UnusedImport],
);
}

View file

@ -65,14 +65,14 @@ impl Violation for ImportShadowedByLoopVar {
}
define_violation!(
pub struct ImportStarUsed {
pub struct ImportStar {
pub name: String,
}
);
impl Violation for ImportStarUsed {
impl Violation for ImportStar {
#[derive_message_formats]
fn message(&self) -> String {
let ImportStarUsed { name } = self;
let ImportStar { name } = self;
format!("`from {name} import *` used; unable to detect undefined names")
}
}

View file

@ -8,8 +8,8 @@ pub use f_string_missing_placeholders::{
pub use forward_annotation_syntax_error::ForwardAnnotationSyntaxError;
pub use if_tuple::{if_tuple, IfTuple};
pub use imports::{
future_feature_not_defined, FutureFeatureNotDefined, ImportShadowedByLoopVar,
ImportStarNotPermitted, ImportStarUsage, ImportStarUsed, LateFutureImport, UnusedImport,
future_feature_not_defined, FutureFeatureNotDefined, ImportShadowedByLoopVar, ImportStar,
ImportStarNotPermitted, ImportStarUsage, LateFutureImport, UnusedImport,
};
pub use invalid_literal_comparisons::{invalid_literal_comparison, IsLiteral};
pub use invalid_print_syntax::{invalid_print_syntax, InvalidPrintSyntax};

View file

@ -1,9 +1,9 @@
---
source: src/rules/pyflakes/mod.rs
source: crates/ruff/src/rules/pyflakes/mod.rs
expression: diagnostics
---
- kind:
ImportStarUsed:
ImportStar:
name: F634
location:
row: 1
@ -14,7 +14,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ImportStarUsed:
ImportStar:
name: F634
location:
row: 2