mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:53 +00:00
Fix F841 (UnusedVariable
) range in except handler (#1367)
This commit is contained in:
parent
cc2110449c
commit
10f75c9620
4 changed files with 32 additions and 11 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
use itertools::Itertools;
|
||||||
use log::error;
|
use log::error;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
@ -396,6 +397,23 @@ pub fn identifier_range(stmt: &Stmt, locator: &SourceCodeLocator) -> Range {
|
||||||
Range::from_located(stmt)
|
Range::from_located(stmt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the `Range` of `name` in `Excepthandler`.
|
||||||
|
pub fn excepthandler_name_range(
|
||||||
|
handler: &Excepthandler,
|
||||||
|
locator: &SourceCodeLocator,
|
||||||
|
) -> Option<Range> {
|
||||||
|
let contents = locator.slice_source_code_range(&Range::from_located(handler));
|
||||||
|
let range = lexer::make_tokenizer(&contents)
|
||||||
|
.flatten()
|
||||||
|
.tuple_windows()
|
||||||
|
.find(|(tok, next_tok)| matches!(tok.1, Tok::As) && matches!(next_tok.1, Tok::Name { .. }))
|
||||||
|
.map(|((..), (start, _, end))| Range {
|
||||||
|
location: to_absolute(start, handler.location),
|
||||||
|
end_location: to_absolute(end, handler.location),
|
||||||
|
});
|
||||||
|
range
|
||||||
|
}
|
||||||
|
|
||||||
/// Return `true` if a `Stmt` appears to be part of a multi-statement line, with
|
/// Return `true` if a `Stmt` appears to be part of a multi-statement line, with
|
||||||
/// other statements preceding it.
|
/// other statements preceding it.
|
||||||
pub fn preceded_by_continuation(stmt: &Stmt, locator: &SourceCodeLocator) -> bool {
|
pub fn preceded_by_continuation(stmt: &Stmt, locator: &SourceCodeLocator) -> bool {
|
||||||
|
|
|
@ -2664,12 +2664,15 @@ where
|
||||||
|
|
||||||
self.check_builtin_shadowing(name, excepthandler, false);
|
self.check_builtin_shadowing(name, excepthandler, false);
|
||||||
|
|
||||||
|
let name_range =
|
||||||
|
helpers::excepthandler_name_range(excepthandler, self.locator).unwrap();
|
||||||
|
|
||||||
if self.current_scope().values.contains_key(&name.as_str()) {
|
if self.current_scope().values.contains_key(&name.as_str()) {
|
||||||
self.handle_node_store(
|
self.handle_node_store(
|
||||||
name,
|
name,
|
||||||
&Expr::new(
|
&Expr::new(
|
||||||
excepthandler.location,
|
name_range.location,
|
||||||
excepthandler.end_location.unwrap(),
|
name_range.end_location,
|
||||||
ExprKind::Name {
|
ExprKind::Name {
|
||||||
id: name.to_string(),
|
id: name.to_string(),
|
||||||
ctx: ExprContext::Store,
|
ctx: ExprContext::Store,
|
||||||
|
@ -2682,8 +2685,8 @@ where
|
||||||
self.handle_node_store(
|
self.handle_node_store(
|
||||||
name,
|
name,
|
||||||
&Expr::new(
|
&Expr::new(
|
||||||
excepthandler.location,
|
name_range.location,
|
||||||
excepthandler.end_location.unwrap(),
|
name_range.end_location,
|
||||||
ExprKind::Name {
|
ExprKind::Name {
|
||||||
id: name.to_string(),
|
id: name.to_string(),
|
||||||
ctx: ExprContext::Store,
|
ctx: ExprContext::Store,
|
||||||
|
@ -2702,7 +2705,7 @@ where
|
||||||
if self.settings.enabled.contains(&CheckCode::F841) {
|
if self.settings.enabled.contains(&CheckCode::F841) {
|
||||||
self.add_check(Check::new(
|
self.add_check(Check::new(
|
||||||
CheckKind::UnusedVariable(name.to_string()),
|
CheckKind::UnusedVariable(name.to_string()),
|
||||||
Range::from_located(excepthandler),
|
name_range,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,10 @@ expression: checks
|
||||||
UnusedVariable: e
|
UnusedVariable: e
|
||||||
location:
|
location:
|
||||||
row: 3
|
row: 3
|
||||||
column: 0
|
column: 21
|
||||||
end_location:
|
end_location:
|
||||||
row: 4
|
row: 3
|
||||||
column: 8
|
column: 22
|
||||||
fix: ~
|
fix: ~
|
||||||
- kind:
|
- kind:
|
||||||
UnusedVariable: z
|
UnusedVariable: z
|
||||||
|
|
|
@ -6,10 +6,10 @@ expression: checks
|
||||||
UnusedVariable: e
|
UnusedVariable: e
|
||||||
location:
|
location:
|
||||||
row: 3
|
row: 3
|
||||||
column: 0
|
column: 21
|
||||||
end_location:
|
end_location:
|
||||||
row: 4
|
row: 3
|
||||||
column: 8
|
column: 22
|
||||||
fix: ~
|
fix: ~
|
||||||
- kind:
|
- kind:
|
||||||
UnusedVariable: foo
|
UnusedVariable: foo
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue