mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 22:01:47 +00:00
Implement F707 (DefaultExceptNotLast) (#101)
This commit is contained in:
parent
8f6ab8b37a
commit
b86414dc7a
9 changed files with 119 additions and 0 deletions
|
@ -118,6 +118,7 @@ OPTIONS:
|
||||||
| F634 | IfTuple | If test is a tuple, which is always `True` |
|
| F634 | IfTuple | If test is a tuple, which is always `True` |
|
||||||
| F704 | YieldOutsideFunction | a `yield` or `yield from` statement outside of a function/method |
|
| F704 | YieldOutsideFunction | a `yield` or `yield from` statement outside of a function/method |
|
||||||
| F706 | ReturnOutsideFunction | a `return` statement outside of a function/method |
|
| F706 | ReturnOutsideFunction | a `return` statement outside of a function/method |
|
||||||
|
| F707 | DefaultExceptNotLast | an `except:` block as not the last exception handler |
|
||||||
| F821 | UndefinedName | Undefined name `...` |
|
| F821 | UndefinedName | Undefined name `...` |
|
||||||
| F822 | UndefinedExport | Undefined name `...` in `__all__` |
|
| F822 | UndefinedExport | Undefined name `...` in `__all__` |
|
||||||
| F823 | UndefinedLocal | Local variable `...` referenced before assignment |
|
| F823 | UndefinedLocal | Local variable `...` referenced before assignment |
|
||||||
|
|
|
@ -4,6 +4,7 @@ use ruff::checks::CheckKind;
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut check_kinds: Vec<CheckKind> = vec![
|
let mut check_kinds: Vec<CheckKind> = vec![
|
||||||
CheckKind::AssertTuple,
|
CheckKind::AssertTuple,
|
||||||
|
CheckKind::DefaultExceptNotLast,
|
||||||
CheckKind::DuplicateArgumentName,
|
CheckKind::DuplicateArgumentName,
|
||||||
CheckKind::FStringMissingPlaceholders,
|
CheckKind::FStringMissingPlaceholders,
|
||||||
CheckKind::IfTuple,
|
CheckKind::IfTuple,
|
||||||
|
|
46
resources/test/fixtures/F707.py
vendored
Normal file
46
resources/test/fixtures/F707.py
vendored
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
try:
|
||||||
|
pass
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
pass
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
pass
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
pass
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
pass
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
pass
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
pass
|
1
resources/test/fixtures/pyproject.toml
vendored
1
resources/test/fixtures/pyproject.toml
vendored
|
@ -10,6 +10,7 @@ select = [
|
||||||
"F634",
|
"F634",
|
||||||
"F704",
|
"F704",
|
||||||
"F706",
|
"F706",
|
||||||
|
"F707",
|
||||||
"F821",
|
"F821",
|
||||||
"F822",
|
"F822",
|
||||||
"F823",
|
"F823",
|
||||||
|
|
|
@ -295,6 +295,23 @@ impl Visitor for Checker<'_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
StmtKind::Try { handlers, .. } => {
|
||||||
|
if self
|
||||||
|
.settings
|
||||||
|
.select
|
||||||
|
.contains(CheckKind::DefaultExceptNotLast.code())
|
||||||
|
{
|
||||||
|
for (idx, handler) in handlers.iter().enumerate() {
|
||||||
|
let ExcepthandlerKind::ExceptHandler { type_, .. } = &handler.node;
|
||||||
|
if type_.is_none() && idx < handlers.len() - 1 {
|
||||||
|
self.checks.push(Check {
|
||||||
|
kind: CheckKind::DefaultExceptNotLast,
|
||||||
|
location: handler.location,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ pub enum CheckCode {
|
||||||
F634,
|
F634,
|
||||||
F704,
|
F704,
|
||||||
F706,
|
F706,
|
||||||
|
F707,
|
||||||
F821,
|
F821,
|
||||||
F822,
|
F822,
|
||||||
F823,
|
F823,
|
||||||
|
@ -38,6 +39,7 @@ impl FromStr for CheckCode {
|
||||||
"F634" => Ok(CheckCode::F634),
|
"F634" => Ok(CheckCode::F634),
|
||||||
"F704" => Ok(CheckCode::F704),
|
"F704" => Ok(CheckCode::F704),
|
||||||
"F706" => Ok(CheckCode::F706),
|
"F706" => Ok(CheckCode::F706),
|
||||||
|
"F707" => Ok(CheckCode::F707),
|
||||||
"F821" => Ok(CheckCode::F821),
|
"F821" => Ok(CheckCode::F821),
|
||||||
"F822" => Ok(CheckCode::F822),
|
"F822" => Ok(CheckCode::F822),
|
||||||
"F823" => Ok(CheckCode::F823),
|
"F823" => Ok(CheckCode::F823),
|
||||||
|
@ -61,6 +63,7 @@ impl CheckCode {
|
||||||
CheckCode::F634 => "F634",
|
CheckCode::F634 => "F634",
|
||||||
CheckCode::F704 => "F704",
|
CheckCode::F704 => "F704",
|
||||||
CheckCode::F706 => "F706",
|
CheckCode::F706 => "F706",
|
||||||
|
CheckCode::F707 => "F707",
|
||||||
CheckCode::F821 => "F821",
|
CheckCode::F821 => "F821",
|
||||||
CheckCode::F823 => "F823",
|
CheckCode::F823 => "F823",
|
||||||
CheckCode::F822 => "F822",
|
CheckCode::F822 => "F822",
|
||||||
|
@ -82,6 +85,7 @@ impl CheckCode {
|
||||||
CheckCode::F634 => &LintSource::AST,
|
CheckCode::F634 => &LintSource::AST,
|
||||||
CheckCode::F704 => &LintSource::AST,
|
CheckCode::F704 => &LintSource::AST,
|
||||||
CheckCode::F706 => &LintSource::AST,
|
CheckCode::F706 => &LintSource::AST,
|
||||||
|
CheckCode::F707 => &LintSource::AST,
|
||||||
CheckCode::F821 => &LintSource::AST,
|
CheckCode::F821 => &LintSource::AST,
|
||||||
CheckCode::F822 => &LintSource::AST,
|
CheckCode::F822 => &LintSource::AST,
|
||||||
CheckCode::F823 => &LintSource::AST,
|
CheckCode::F823 => &LintSource::AST,
|
||||||
|
@ -109,6 +113,7 @@ pub enum CheckKind {
|
||||||
LineTooLong,
|
LineTooLong,
|
||||||
RaiseNotImplemented,
|
RaiseNotImplemented,
|
||||||
ReturnOutsideFunction,
|
ReturnOutsideFunction,
|
||||||
|
DefaultExceptNotLast,
|
||||||
UndefinedLocal(String),
|
UndefinedLocal(String),
|
||||||
UndefinedExport(String),
|
UndefinedExport(String),
|
||||||
UndefinedName(String),
|
UndefinedName(String),
|
||||||
|
@ -130,6 +135,7 @@ impl CheckKind {
|
||||||
CheckKind::LineTooLong => "LineTooLong",
|
CheckKind::LineTooLong => "LineTooLong",
|
||||||
CheckKind::RaiseNotImplemented => "RaiseNotImplemented",
|
CheckKind::RaiseNotImplemented => "RaiseNotImplemented",
|
||||||
CheckKind::ReturnOutsideFunction => "ReturnOutsideFunction",
|
CheckKind::ReturnOutsideFunction => "ReturnOutsideFunction",
|
||||||
|
CheckKind::DefaultExceptNotLast => "DefaultExceptNotLast",
|
||||||
CheckKind::UndefinedLocal(_) => "UndefinedLocal",
|
CheckKind::UndefinedLocal(_) => "UndefinedLocal",
|
||||||
CheckKind::UndefinedExport(_) => "UndefinedExport",
|
CheckKind::UndefinedExport(_) => "UndefinedExport",
|
||||||
CheckKind::UndefinedName(_) => "UndefinedName",
|
CheckKind::UndefinedName(_) => "UndefinedName",
|
||||||
|
@ -151,6 +157,7 @@ impl CheckKind {
|
||||||
CheckKind::LineTooLong => &CheckCode::E501,
|
CheckKind::LineTooLong => &CheckCode::E501,
|
||||||
CheckKind::RaiseNotImplemented => &CheckCode::F901,
|
CheckKind::RaiseNotImplemented => &CheckCode::F901,
|
||||||
CheckKind::ReturnOutsideFunction => &CheckCode::F706,
|
CheckKind::ReturnOutsideFunction => &CheckCode::F706,
|
||||||
|
CheckKind::DefaultExceptNotLast => &CheckCode::F707,
|
||||||
CheckKind::UndefinedExport(_) => &CheckCode::F822,
|
CheckKind::UndefinedExport(_) => &CheckCode::F822,
|
||||||
CheckKind::UndefinedLocal(_) => &CheckCode::F823,
|
CheckKind::UndefinedLocal(_) => &CheckCode::F823,
|
||||||
CheckKind::UndefinedName(_) => &CheckCode::F821,
|
CheckKind::UndefinedName(_) => &CheckCode::F821,
|
||||||
|
@ -182,6 +189,9 @@ impl CheckKind {
|
||||||
CheckKind::ReturnOutsideFunction => {
|
CheckKind::ReturnOutsideFunction => {
|
||||||
"a `return` statement outside of a function/method".to_string()
|
"a `return` statement outside of a function/method".to_string()
|
||||||
}
|
}
|
||||||
|
CheckKind::DefaultExceptNotLast => {
|
||||||
|
"an `except:` block as not the last exception handler".to_string()
|
||||||
|
}
|
||||||
CheckKind::UndefinedExport(name) => {
|
CheckKind::UndefinedExport(name) => {
|
||||||
format!("Undefined name `{name}` in `__all__`")
|
format!("Undefined name `{name}` in `__all__`")
|
||||||
}
|
}
|
||||||
|
|
|
@ -320,6 +320,47 @@ mod tests {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn f707() -> Result<()> {
|
||||||
|
let actual = check_path(
|
||||||
|
Path::new("./resources/test/fixtures/F707.py"),
|
||||||
|
&settings::Settings {
|
||||||
|
line_length: 88,
|
||||||
|
exclude: vec![],
|
||||||
|
select: BTreeSet::from([CheckCode::F707]),
|
||||||
|
},
|
||||||
|
&cache::Mode::None,
|
||||||
|
)?;
|
||||||
|
let expected = vec![
|
||||||
|
Message {
|
||||||
|
kind: CheckKind::DefaultExceptNotLast,
|
||||||
|
location: Location::new(3, 1),
|
||||||
|
filename: "./resources/test/fixtures/F707.py".to_string(),
|
||||||
|
},
|
||||||
|
Message {
|
||||||
|
kind: CheckKind::DefaultExceptNotLast,
|
||||||
|
location: Location::new(10, 1),
|
||||||
|
filename: "./resources/test/fixtures/F707.py".to_string(),
|
||||||
|
},
|
||||||
|
Message {
|
||||||
|
kind: CheckKind::DefaultExceptNotLast,
|
||||||
|
location: Location::new(19, 1),
|
||||||
|
filename: "./resources/test/fixtures/F707.py".to_string(),
|
||||||
|
},
|
||||||
|
Message {
|
||||||
|
kind: CheckKind::DefaultExceptNotLast,
|
||||||
|
location: Location::new(28, 1),
|
||||||
|
filename: "./resources/test/fixtures/F707.py".to_string(),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
assert_eq!(actual.len(), expected.len());
|
||||||
|
for i in 0..actual.len() {
|
||||||
|
assert_eq!(actual[i], expected[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn f821() -> Result<()> {
|
fn f821() -> Result<()> {
|
||||||
let actual = check_path(
|
let actual = check_path(
|
||||||
|
|
|
@ -245,6 +245,7 @@ other-attribute = 1
|
||||||
CheckCode::F634,
|
CheckCode::F634,
|
||||||
CheckCode::F704,
|
CheckCode::F704,
|
||||||
CheckCode::F706,
|
CheckCode::F706,
|
||||||
|
CheckCode::F707,
|
||||||
CheckCode::F821,
|
CheckCode::F821,
|
||||||
CheckCode::F822,
|
CheckCode::F822,
|
||||||
CheckCode::F823,
|
CheckCode::F823,
|
||||||
|
|
|
@ -51,6 +51,7 @@ impl Settings {
|
||||||
CheckCode::F631,
|
CheckCode::F631,
|
||||||
CheckCode::F634,
|
CheckCode::F634,
|
||||||
CheckCode::F706,
|
CheckCode::F706,
|
||||||
|
CheckCode::F707,
|
||||||
CheckCode::F831,
|
CheckCode::F831,
|
||||||
CheckCode::F823,
|
CheckCode::F823,
|
||||||
CheckCode::F901,
|
CheckCode::F901,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue