Fix F841 (UnusedVariable) range in except handler (#1367)

This commit is contained in:
Harutaka Kawamura 2022-12-25 12:55:55 +09:00 committed by GitHub
parent cc2110449c
commit 10f75c9620
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 11 deletions

View file

@ -1,3 +1,4 @@
use itertools::Itertools;
use log::error;
use once_cell::sync::Lazy;
use regex::Regex;
@ -396,6 +397,23 @@ pub fn identifier_range(stmt: &Stmt, locator: &SourceCodeLocator) -> Range {
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
/// other statements preceding it.
pub fn preceded_by_continuation(stmt: &Stmt, locator: &SourceCodeLocator) -> bool {