From 2cd117ba810d4e239bcc0ee1c95a646a668dd180 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 16 Jul 2023 21:24:16 -0400 Subject: [PATCH] Remove `TryIdentifier` trait (#5816) ## Summary Last remaining usage here is for patterns, but we now have ranges on identifiers so it's unnecessary. --- crates/ruff/src/checkers/ast/mod.rs | 4 +- crates/ruff_python_ast/src/identifier.rs | 59 +----------------------- 2 files changed, 3 insertions(+), 60 deletions(-) diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index 14966f63f7..a974ff14ec 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -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(), ); diff --git a/crates/ruff_python_ast/src/identifier.rs b/crates/ruff_python_ast/src/identifier.rs index 9e38119515..dabbffccbb 100644 --- a/crates/ruff_python_ast/src/identifier.rs +++ b/crates/ruff_python_ast/src/identifier.rs @@ -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; -} - 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 { - 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())