Remove TryIdentifier trait (#5816)

## Summary

Last remaining usage here is for patterns, but we now have ranges on
identifiers so it's unnecessary.
This commit is contained in:
Charlie Marsh 2023-07-16 21:24:16 -04:00 committed by GitHub
parent a956226d95
commit 2cd117ba81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 60 deletions

View file

@ -12,7 +12,7 @@ use rustpython_parser::ast::{
use ruff_diagnostics::{Diagnostic, Fix, IsolationLevel};
use ruff_python_ast::all::{extract_all_names, AllNamesFlags};
use ruff_python_ast::helpers::{extract_handled_exceptions, to_module_path};
use ruff_python_ast::identifier::{Identifier, TryIdentifier};
use ruff_python_ast::identifier::Identifier;
use ruff_python_ast::source_code::{Generator, Indexer, Locator, Quote, Stylist};
use ruff_python_ast::str::trailing_quote;
use ruff_python_ast::types::Node;
@ -4085,7 +4085,7 @@ where
{
self.add_binding(
name,
pattern.try_identifier().unwrap(),
name.range(),
BindingKind::Assignment,
BindingFlags::empty(),
);

View file

@ -11,7 +11,7 @@
//! This module can be used to identify the [`TextRange`] of the `except` token.
use ruff_text_size::{TextLen, TextRange, TextSize};
use rustpython_ast::{Alias, Arg, ArgWithDefault, Pattern};
use rustpython_ast::{Alias, Arg, ArgWithDefault};
use rustpython_parser::ast::{self, ExceptHandler, Ranged, Stmt};
use ruff_python_whitespace::{is_python_whitespace, Cursor};
@ -23,12 +23,6 @@ pub trait Identifier {
fn identifier(&self) -> TextRange;
}
pub trait TryIdentifier {
/// Return the [`TextRange`] of the identifier in the given AST node, or `None` if
/// the node does not have an identifier.
fn try_identifier(&self) -> Option<TextRange>;
}
impl Identifier for Stmt {
/// Return the [`TextRange`] of the identifier in the given statement.
///
@ -87,57 +81,6 @@ impl Identifier for Alias {
}
}
impl TryIdentifier for Pattern {
/// Return the [`TextRange`] of the identifier in the given pattern.
///
/// For example, return the range of `z` in:
/// ```python
/// match x:
/// # Pattern::MatchAs
/// case z:
/// ...
/// ```
///
/// Or:
/// ```python
/// match x:
/// # Pattern::MatchAs
/// case y as z:
/// ...
/// ```
///
/// Or :
/// ```python
/// match x:
/// # Pattern::MatchMapping
/// case {"a": 1, **z}
/// ...
/// ```
///
/// Or :
/// ```python
/// match x:
/// # Pattern::MatchStar
/// case *z:
/// ...
/// ```
fn try_identifier(&self) -> Option<TextRange> {
let name = match self {
Pattern::MatchAs(ast::PatternMatchAs {
name: Some(name), ..
}) => Some(name),
Pattern::MatchMapping(ast::PatternMatchMapping {
rest: Some(rest), ..
}) => Some(rest),
Pattern::MatchStar(ast::PatternMatchStar {
name: Some(name), ..
}) => Some(name),
_ => None,
};
name.map(Ranged::range)
}
}
/// Return the [`TextRange`] of the `except` token in an [`ExceptHandler`].
pub fn except(handler: &ExceptHandler, locator: &Locator) -> TextRange {
IdentifierTokenizer::new(locator.contents(), handler.range())