Skip to content

Commit

Permalink
Use smol_str for Identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jun 29, 2024
1 parent 47b2273 commit 31c6c03
Show file tree
Hide file tree
Showing 456 changed files with 13,675 additions and 4,689 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/red_knot_python_semantic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod ast_node_ref;
mod db;
pub mod name;
mod node_key;
pub mod semantic_index;
pub mod types;
Expand Down
56 changes: 0 additions & 56 deletions crates/red_knot_python_semantic/src/name.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use rustc_hash::FxHashMap;
use ruff_db::parsed::ParsedModule;
use ruff_index::IndexVec;
use ruff_python_ast as ast;
use ruff_python_ast::name::Name;
use ruff_python_ast::visitor::{walk_expr, walk_stmt, Visitor};

use crate::name::Name;
use crate::node_key::NodeKey;
use crate::semantic_index::ast_ids::{
AstId, AstIdsBuilder, ScopeAssignmentId, ScopeClassId, ScopeFunctionId, ScopeImportFromId,
Expand Down
7 changes: 3 additions & 4 deletions crates/red_knot_python_semantic/src/semantic_index/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ use rustc_hash::FxHasher;
use salsa::DebugWithDb;
use smallvec::SmallVec;

use ruff_db::vfs::VfsFile;
use ruff_index::{newtype_index, IndexVec};

use crate::name::Name;
use crate::semantic_index::definition::Definition;
use crate::semantic_index::{root_scope, semantic_index, symbol_table, SymbolMap};
use crate::Db;
use ruff_db::vfs::VfsFile;
use ruff_index::{newtype_index, IndexVec};
use ruff_python_ast::name::Name;

#[derive(Eq, PartialEq, Debug)]
pub struct Symbol {
Expand Down
11 changes: 5 additions & 6 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
use salsa::DebugWithDb;

use ruff_db::parsed::parsed_module;
use ruff_db::vfs::VfsFile;
use ruff_index::newtype_index;
use ruff_python_ast as ast;

use crate::name::Name;
use crate::semantic_index::ast_ids::{AstIdNode, ScopeAstIdNode};
use crate::semantic_index::symbol::{FileScopeId, PublicSymbolId, ScopeId};
use crate::semantic_index::{
Expand All @@ -14,6 +8,11 @@ use crate::semantic_index::{
use crate::types::infer::{TypeInference, TypeInferenceBuilder};
use crate::Db;
use crate::FxIndexSet;
use ruff_db::parsed::parsed_module;
use ruff_db::vfs::VfsFile;
use ruff_index::newtype_index;
use ruff_python_ast as ast;
use ruff_python_ast::name::Name;

mod display;
mod infer;
Expand Down
13 changes: 6 additions & 7 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use ruff_index::IndexVec;
use ruff_python_ast as ast;
use ruff_python_ast::{ExprContext, TypeParams};

use crate::name::Name;
use crate::semantic_index::ast_ids::{ScopeAstIdNode, ScopeExpressionId};
use crate::semantic_index::definition::{Definition, ImportDefinition, ImportFromDefinition};
use crate::semantic_index::symbol::{FileScopeId, ScopeId, ScopeKind, ScopedSymbolId, SymbolTable};
Expand Down Expand Up @@ -199,7 +198,7 @@ impl<'db> TypeInferenceBuilder<'db> {
}

let function_ty = self.function_ty(FunctionType {
name: Name::new(&name.id),
name: name.id.clone(),
decorators: decorator_tys,
});

Expand Down Expand Up @@ -248,7 +247,7 @@ impl<'db> TypeInferenceBuilder<'db> {
assert_eq!(class_body_scope.kind(), ScopeKind::Class);

let class_ty = self.class_ty(ClassType {
name: Name::new(name),
name: name.id.clone(),
bases,
body_scope: class_body_scope_id.to_scope_id(self.db, self.file_id),
});
Expand Down Expand Up @@ -398,7 +397,7 @@ impl<'db> TypeInferenceBuilder<'db> {
} = alias;

let ty = module_ty
.member(&self.typing_context(), &Name::new(&name.id))
.member(&self.typing_context(), &name.id)
.unwrap_or(Type::Unknown);

self.definition_tys.insert(
Expand Down Expand Up @@ -557,7 +556,7 @@ impl<'db> TypeInferenceBuilder<'db> {

let value_ty = self.infer_expression(value);
let member_ty = value_ty
.member(&self.typing_context(), &Name::new(&attr.id))
.member(&self.typing_context(), &attr.id)
.unwrap_or(Type::Unknown);

match ctx {
Expand Down Expand Up @@ -695,9 +694,9 @@ mod tests {
use ruff_db::vfs::system_path_to_file;

use crate::db::tests::TestDb;
use crate::name::Name;
use crate::types::{public_symbol_ty_by_name, Type, TypingContext};
use red_knot_module_resolver::{set_module_resolution_settings, ModuleResolutionSettings};
use ruff_python_ast::name::Name;

fn setup_db() -> TestDb {
let mut db = TestDb::new();
Expand Down Expand Up @@ -791,7 +790,7 @@ class C:
};

let context = TypingContext::global(&db);
let member_ty = class_id.class_member(&context, &Name::new("f"));
let member_ty = class_id.class_member(&context, &Name::new_static("f"));

let Some(Type::Function(func_id)) = member_ty else {
panic!("C.f is not a Function");
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/fix/edits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub(crate) fn make_redundant_alias<'a>(
.filter_map(|name| {
aliases
.iter()
.find(|alias| alias.asname.is_none() && name == alias.name.id)
.find(|alias| alias.asname.is_none() && *name == alias.name.id)
.map(|alias| Edit::range_replacement(format!("{name} as {name}"), alias.range))
})
.collect()
Expand Down
10 changes: 5 additions & 5 deletions crates/ruff_linter/src/importer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
use std::error::Error;

use anyhow::Result;
use libcst_native::{ImportAlias, Name, NameOrAttribute};
use ruff_python_ast::{self as ast, ModModule, Stmt};
use ruff_python_parser::{Parsed, Tokens};
use ruff_text_size::{Ranged, TextSize};
use libcst_native::{ImportAlias, Name as cstName, NameOrAttribute};

use ruff_diagnostics::Edit;
use ruff_python_ast::imports::{AnyImport, Import, ImportFrom};
use ruff_python_ast::{self as ast, ModModule, Stmt};
use ruff_python_codegen::Stylist;
use ruff_python_parser::{Parsed, Tokens};
use ruff_python_semantic::{ImportedName, SemanticModel};
use ruff_python_trivia::textwrap::indent;
use ruff_source_file::Locator;
use ruff_text_size::{Ranged, TextSize};

use crate::cst::matchers::{match_aliases, match_import_from, match_statement};
use crate::fix;
Expand Down Expand Up @@ -425,7 +425,7 @@ impl<'a> Importer<'a> {
let import_from = match_import_from(&mut statement)?;
let aliases = match_aliases(import_from)?;
aliases.push(ImportAlias {
name: NameOrAttribute::N(Box::new(Name {
name: NameOrAttribute::N(Box::new(cstName {
value: member,
lpar: vec![],
rpar: vec![],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub(crate) fn variable_name_task_id(
let ast::ExprStringLiteral { value: task_id, .. } = keyword.value.as_string_literal_expr()?;

// If the target name is the same as the task_id, no violation.
if task_id == id {
if task_id == id.as_str() {
return None;
}

Expand Down
7 changes: 4 additions & 3 deletions crates/ruff_linter/src/rules/flake8_annotations/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ruff_diagnostics::Edit;
use ruff_python_ast::helpers::{
pep_604_union, typing_optional, typing_union, ReturnStatementVisitor,
};
use ruff_python_ast::name::Name;
use ruff_python_ast::visitor::Visitor;
use ruff_python_ast::{self as ast, Expr, ExprContext};
use ruff_python_semantic::analyze::terminal::Terminal;
Expand Down Expand Up @@ -140,7 +141,7 @@ impl AutoPythonType {
)
.ok()?;
let expr = Expr::Name(ast::ExprName {
id: binding,
id: Name::from(binding),
range: TextRange::default(),
ctx: ExprContext::Load,
});
Expand Down Expand Up @@ -181,7 +182,7 @@ impl AutoPythonType {
semantic,
)
.ok()?;
let expr = typing_optional(element, binding);
let expr = typing_optional(element, Name::from(binding));
Some((expr, vec![optional_edit]))
}
_ => {
Expand All @@ -198,7 +199,7 @@ impl AutoPythonType {
semantic,
)
.ok()?;
let expr = typing_union(&elements, binding);
let expr = typing_union(&elements, Name::from(binding));
Some((expr, vec![union_edit]))
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff_linter/src/rules/flake8_gettext/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Rules from [flake8-gettext](https://pypi.org/project/flake8-gettext/).
use ruff_python_ast::name::Name;
use ruff_python_ast::{self as ast, Expr};

pub(crate) mod rules;
pub mod settings;

/// Returns true if the [`Expr`] is an internationalization function call.
pub(crate) fn is_gettext_func_call(func: &Expr, functions_names: &[String]) -> bool {
pub(crate) fn is_gettext_func_call(func: &Expr, functions_names: &[Name]) -> bool {
if let Expr::Name(ast::ExprName { id, .. }) = func {
functions_names.contains(id)
} else {
Expand Down
11 changes: 6 additions & 5 deletions crates/ruff_linter/src/rules/flake8_gettext/settings.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use crate::display_settings;
use ruff_macros::CacheKey;
use ruff_python_ast::name::Name;
use std::fmt::{Display, Formatter};

#[derive(Debug, Clone, CacheKey)]
pub struct Settings {
pub functions_names: Vec<String>,
pub functions_names: Vec<Name>,
}

pub fn default_func_names() -> Vec<String> {
pub fn default_func_names() -> Vec<Name> {
vec![
"_".to_string(),
"gettext".to_string(),
"ngettext".to_string(),
Name::new_static("_"),
Name::new_static("gettext"),
Name::new_static("ngettext"),
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ast::ExprContext;
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::pep_604_union;
use ruff_python_ast::name::Name;
use ruff_python_ast::{self as ast, Expr};
use ruff_python_semantic::analyze::typing::traverse_union;
use ruff_text_size::{Ranged, TextRange};
Expand All @@ -26,7 +27,7 @@ use crate::checkers::ast::Checker;
/// ```
#[violation]
pub struct UnnecessaryTypeUnion {
members: Vec<String>,
members: Vec<Name>,
is_pep604_union: bool,
}

Expand Down Expand Up @@ -83,10 +84,10 @@ pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr)
traverse_union(&mut collect_type_exprs, semantic, union);

if type_exprs.len() > 1 {
let type_members: Vec<String> = type_exprs
let type_members: Vec<Name> = type_exprs
.clone()
.into_iter()
.map(|type_expr| checker.locator().slice(type_expr).to_string())
.map(|type_expr| Name::new(checker.locator().slice(type_expr)))
.collect();

let mut diagnostic = Diagnostic::new(
Expand All @@ -101,7 +102,7 @@ pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr)
let content = if let Some(subscript) = subscript {
let types = &Expr::Subscript(ast::ExprSubscript {
value: Box::new(Expr::Name(ast::ExprName {
id: "type".into(),
id: Name::new_static("type"),
ctx: ExprContext::Load,
range: TextRange::default(),
})),
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_linter/src/rules/flake8_self/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ mod tests {
use std::convert::AsRef;
use std::path::Path;

use anyhow::Result;
use test_case::test_case;

use crate::registry::Rule;
use crate::rules::flake8_self;
use crate::test::test_path;
use crate::{assert_messages, settings};
use anyhow::Result;
use ruff_python_ast::name::Name;
use test_case::test_case;

#[test_case(Rule::PrivateMemberAccess, Path::new("SLF001.py"))]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
Expand All @@ -32,7 +32,7 @@ mod tests {
Path::new("flake8_self/SLF001_extended.py"),
&settings::LinterSettings {
flake8_self: flake8_self::settings::Settings {
ignore_names: vec!["_meta".to_string()],
ignore_names: vec![Name::new_static("_meta")],
},
..settings::LinterSettings::for_rule(Rule::PrivateMemberAccess)
},
Expand Down
Loading

0 comments on commit 31c6c03

Please sign in to comment.