mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 13:33:50 +00:00
Remove some usize
references (#3819)
This commit is contained in:
parent
9de1f82658
commit
cf7e1ddd08
13 changed files with 43 additions and 53 deletions
|
@ -1113,7 +1113,7 @@ where
|
|||
stmt,
|
||||
names,
|
||||
module.as_ref().map(String::as_str),
|
||||
level.as_ref(),
|
||||
*level,
|
||||
);
|
||||
}
|
||||
if self.settings.rules.enabled(Rule::UnnecessaryBuiltinImport) {
|
||||
|
@ -1152,11 +1152,9 @@ where
|
|||
.rules
|
||||
.enabled(Rule::PytestIncorrectPytestImport)
|
||||
{
|
||||
if let Some(diagnostic) = flake8_pytest_style::rules::import_from(
|
||||
stmt,
|
||||
module.as_deref(),
|
||||
level.as_ref(),
|
||||
) {
|
||||
if let Some(diagnostic) =
|
||||
flake8_pytest_style::rules::import_from(stmt, module.as_deref(), *level)
|
||||
{
|
||||
self.diagnostics.push(diagnostic);
|
||||
}
|
||||
}
|
||||
|
@ -1223,7 +1221,7 @@ where
|
|||
self.diagnostics.push(Diagnostic::new(
|
||||
pyflakes::rules::UndefinedLocalWithNestedImportStarUsage {
|
||||
name: helpers::format_import_from(
|
||||
level.as_ref(),
|
||||
*level,
|
||||
module.as_deref(),
|
||||
),
|
||||
},
|
||||
|
@ -1239,10 +1237,7 @@ where
|
|||
{
|
||||
self.diagnostics.push(Diagnostic::new(
|
||||
pyflakes::rules::UndefinedLocalWithImportStar {
|
||||
name: helpers::format_import_from(
|
||||
level.as_ref(),
|
||||
module.as_deref(),
|
||||
),
|
||||
name: helpers::format_import_from(*level, module.as_deref()),
|
||||
},
|
||||
Range::from(stmt),
|
||||
));
|
||||
|
@ -1268,7 +1263,7 @@ where
|
|||
// and `full_name` would be "foo.bar".
|
||||
let name = alias.node.asname.as_ref().unwrap_or(&alias.node.name);
|
||||
let full_name = helpers::format_import_from_member(
|
||||
level.as_ref(),
|
||||
*level,
|
||||
module.as_deref(),
|
||||
&alias.node.name,
|
||||
);
|
||||
|
@ -1299,7 +1294,7 @@ where
|
|||
flake8_tidy_imports::relative_imports::banned_relative_import(
|
||||
self,
|
||||
stmt,
|
||||
level.as_ref(),
|
||||
*level,
|
||||
module.as_deref(),
|
||||
self.module_path.as_ref(),
|
||||
&self.settings.flake8_tidy_imports.ban_relative_imports,
|
||||
|
@ -1322,7 +1317,7 @@ where
|
|||
|
||||
if self.settings.rules.enabled(Rule::UnconventionalImportAlias) {
|
||||
let full_name = helpers::format_import_from_member(
|
||||
level.as_ref(),
|
||||
*level,
|
||||
module.as_deref(),
|
||||
&alias.node.name,
|
||||
);
|
||||
|
@ -4273,10 +4268,7 @@ impl<'a> Checker<'a> {
|
|||
if let BindingKind::StarImportation(StarImportation { level, module }) =
|
||||
&binding.kind
|
||||
{
|
||||
from_list.push(helpers::format_import_from(
|
||||
level.as_ref(),
|
||||
module.as_deref(),
|
||||
));
|
||||
from_list.push(helpers::format_import_from(*level, module.as_deref()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4927,10 +4919,8 @@ impl<'a> Checker<'a> {
|
|||
if let BindingKind::StarImportation(StarImportation { level, module }) =
|
||||
&binding.kind
|
||||
{
|
||||
from_list.push(helpers::format_import_from(
|
||||
level.as_ref(),
|
||||
module.as_deref(),
|
||||
));
|
||||
from_list
|
||||
.push(helpers::format_import_from(*level, module.as_deref()));
|
||||
}
|
||||
}
|
||||
from_list.sort();
|
||||
|
|
|
@ -37,11 +37,11 @@ pub fn import(import_from: &Stmt, name: &str, asname: Option<&str>) -> Option<Di
|
|||
pub fn import_from(
|
||||
import_from: &Stmt,
|
||||
module: Option<&str>,
|
||||
level: Option<&usize>,
|
||||
level: Option<usize>,
|
||||
) -> Option<Diagnostic> {
|
||||
// If level is not zero or module is none, return
|
||||
if let Some(level) = level {
|
||||
if *level != 0 {
|
||||
if level != 0 {
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -89,19 +89,19 @@ impl Violation for RelativeImports {
|
|||
|
||||
fn fix_banned_relative_import(
|
||||
stmt: &Stmt,
|
||||
level: Option<&usize>,
|
||||
level: Option<usize>,
|
||||
module: Option<&str>,
|
||||
module_path: Option<&Vec<String>>,
|
||||
stylist: &Stylist,
|
||||
) -> Option<Edit> {
|
||||
// Only fix is the module path is known.
|
||||
if let Some(mut parts) = module_path.cloned() {
|
||||
if *level? >= parts.len() {
|
||||
if level? >= parts.len() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Remove relative level from module path.
|
||||
for _ in 0..*level? {
|
||||
for _ in 0..level? {
|
||||
parts.pop();
|
||||
}
|
||||
|
||||
|
@ -166,7 +166,7 @@ fn fix_banned_relative_import(
|
|||
pub fn banned_relative_import(
|
||||
checker: &Checker,
|
||||
stmt: &Stmt,
|
||||
level: Option<&usize>,
|
||||
level: Option<usize>,
|
||||
module: Option<&str>,
|
||||
module_path: Option<&Vec<String>>,
|
||||
strictness: &Strictness,
|
||||
|
@ -175,7 +175,7 @@ pub fn banned_relative_import(
|
|||
Strictness::All => 0,
|
||||
Strictness::Parents => 1,
|
||||
};
|
||||
if level? > &strictness_level {
|
||||
if level? > strictness_level {
|
||||
let mut diagnostic = Diagnostic::new(
|
||||
RelativeImports {
|
||||
strictness: *strictness,
|
||||
|
|
|
@ -183,7 +183,7 @@ pub fn typing_only_runtime_import(
|
|||
// Categorize the import.
|
||||
match categorize(
|
||||
full_name,
|
||||
Some(&level),
|
||||
Some(level),
|
||||
&settings.src,
|
||||
package,
|
||||
&settings.isort.known_modules,
|
||||
|
|
|
@ -106,7 +106,7 @@ pub fn annotate_imports<'a>(
|
|||
annotated.push(AnnotatedImport::ImportFrom {
|
||||
module: module.as_deref(),
|
||||
names: aliases,
|
||||
level: level.as_ref(),
|
||||
level: *level,
|
||||
trailing_comma: if split_on_trailing_comma {
|
||||
trailing_comma(import, locator)
|
||||
} else {
|
||||
|
|
|
@ -54,7 +54,7 @@ enum Reason<'a> {
|
|||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn categorize(
|
||||
module_name: &str,
|
||||
level: Option<&usize>,
|
||||
level: Option<usize>,
|
||||
src: &[PathBuf],
|
||||
package: Option<&Path>,
|
||||
known_modules: &KnownModules,
|
||||
|
@ -62,7 +62,7 @@ pub fn categorize(
|
|||
) -> ImportType {
|
||||
let module_base = module_name.split('.').next().unwrap();
|
||||
let (import_type, reason) = {
|
||||
if level.map_or(false, |level| *level > 0) {
|
||||
if level.map_or(false, |level| level > 0) {
|
||||
(ImportType::LocalFolder, Reason::NonZeroLevel)
|
||||
} else if module_base == "__future__" {
|
||||
(ImportType::Future, Reason::Future)
|
||||
|
|
|
@ -55,7 +55,7 @@ pub enum AnnotatedImport<'a> {
|
|||
ImportFrom {
|
||||
module: Option<&'a str>,
|
||||
names: Vec<AnnotatedAliasData<'a>>,
|
||||
level: Option<&'a usize>,
|
||||
level: Option<usize>,
|
||||
atop: Vec<Comment<'a>>,
|
||||
inline: Vec<Comment<'a>>,
|
||||
trailing_comma: TrailingComma,
|
||||
|
|
|
@ -95,8 +95,8 @@ pub fn cmp_members(
|
|||
|
||||
/// Compare two relative import levels.
|
||||
pub fn cmp_levels(
|
||||
level1: Option<&usize>,
|
||||
level2: Option<&usize>,
|
||||
level1: Option<usize>,
|
||||
level2: Option<usize>,
|
||||
relative_imports_order: RelativeImportsOrder,
|
||||
) -> Ordering {
|
||||
match (level1, level2) {
|
||||
|
@ -104,8 +104,8 @@ pub fn cmp_levels(
|
|||
(None, Some(_)) => Ordering::Less,
|
||||
(Some(_), None) => Ordering::Greater,
|
||||
(Some(level1), Some(level2)) => match relative_imports_order {
|
||||
RelativeImportsOrder::ClosestToFurthest => level1.cmp(level2),
|
||||
RelativeImportsOrder::FurthestToClosest => level2.cmp(level1),
|
||||
RelativeImportsOrder::ClosestToFurthest => level1.cmp(&level2),
|
||||
RelativeImportsOrder::FurthestToClosest => level2.cmp(&level1),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ pub enum TrailingComma {
|
|||
#[derive(Debug, Hash, Ord, PartialOrd, Eq, PartialEq, Clone)]
|
||||
pub struct ImportFromData<'a> {
|
||||
pub module: Option<&'a str>,
|
||||
pub level: Option<&'a usize>,
|
||||
pub level: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
|
||||
|
|
|
@ -516,10 +516,10 @@ pub fn deprecated_import(
|
|||
stmt: &Stmt,
|
||||
names: &[Alias],
|
||||
module: Option<&str>,
|
||||
level: Option<&usize>,
|
||||
level: Option<usize>,
|
||||
) {
|
||||
// Avoid relative and star imports.
|
||||
if level.map_or(false, |level| *level > 0) {
|
||||
if level.map_or(false, |level| level > 0) {
|
||||
return;
|
||||
}
|
||||
if names.first().map_or(false, |name| name.node.name == "*") {
|
||||
|
|
|
@ -368,7 +368,7 @@ pub struct ComparableComprehension<'a> {
|
|||
pub target: ComparableExpr<'a>,
|
||||
pub iter: ComparableExpr<'a>,
|
||||
pub ifs: Vec<ComparableExpr<'a>>,
|
||||
pub is_async: &'a usize,
|
||||
pub is_async: usize,
|
||||
}
|
||||
|
||||
impl<'a> From<&'a Comprehension> for ComparableComprehension<'a> {
|
||||
|
@ -377,7 +377,7 @@ impl<'a> From<&'a Comprehension> for ComparableComprehension<'a> {
|
|||
target: (&comprehension.target).into(),
|
||||
iter: (&comprehension.iter).into(),
|
||||
ifs: comprehension.ifs.iter().map(Into::into).collect(),
|
||||
is_async: &comprehension.is_async,
|
||||
is_async: comprehension.is_async,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -475,7 +475,7 @@ pub enum ComparableExpr<'a> {
|
|||
},
|
||||
FormattedValue {
|
||||
value: Box<ComparableExpr<'a>>,
|
||||
conversion: &'a usize,
|
||||
conversion: usize,
|
||||
format_spec: Option<Box<ComparableExpr<'a>>>,
|
||||
},
|
||||
JoinedStr {
|
||||
|
@ -623,7 +623,7 @@ impl<'a> From<&'a Expr> for ComparableExpr<'a> {
|
|||
format_spec,
|
||||
} => Self::FormattedValue {
|
||||
value: value.into(),
|
||||
conversion,
|
||||
conversion: *conversion,
|
||||
format_spec: format_spec.as_ref().map(Into::into),
|
||||
},
|
||||
ExprKind::JoinedStr { values } => Self::JoinedStr {
|
||||
|
|
|
@ -711,10 +711,10 @@ pub fn uses_magic_variable_access(ctx: &Context, body: &[Stmt]) -> bool {
|
|||
}
|
||||
|
||||
/// Format the module name for a relative import.
|
||||
pub fn format_import_from(level: Option<&usize>, module: Option<&str>) -> String {
|
||||
pub fn format_import_from(level: Option<usize>, module: Option<&str>) -> String {
|
||||
let mut module_name = String::with_capacity(16);
|
||||
if let Some(level) = level {
|
||||
for _ in 0..*level {
|
||||
for _ in 0..level {
|
||||
module_name.push('.');
|
||||
}
|
||||
}
|
||||
|
@ -726,18 +726,18 @@ pub fn format_import_from(level: Option<&usize>, module: Option<&str>) -> String
|
|||
|
||||
/// Format the member reference name for a relative import.
|
||||
pub fn format_import_from_member(
|
||||
level: Option<&usize>,
|
||||
level: Option<usize>,
|
||||
module: Option<&str>,
|
||||
member: &str,
|
||||
) -> String {
|
||||
let mut full_name = String::with_capacity(
|
||||
level.map_or(0, |level| *level)
|
||||
level.map_or(0, |level| level)
|
||||
+ module.as_ref().map_or(0, |module| module.len())
|
||||
+ 1
|
||||
+ member.len(),
|
||||
);
|
||||
if let Some(level) = level {
|
||||
for _ in 0..*level {
|
||||
for _ in 0..level {
|
||||
full_name.push('.');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -642,13 +642,13 @@ fn format_import_from(
|
|||
stmt: &Stmt,
|
||||
module: Option<&str>,
|
||||
names: &[Alias],
|
||||
level: Option<&usize>,
|
||||
level: Option<usize>,
|
||||
) -> FormatResult<()> {
|
||||
write!(f, [text("from")])?;
|
||||
write!(f, [space()])?;
|
||||
|
||||
if let Some(level) = level {
|
||||
for _ in 0..*level {
|
||||
for _ in 0..level {
|
||||
write!(f, [text(".")])?;
|
||||
}
|
||||
}
|
||||
|
@ -939,7 +939,7 @@ impl Format<ASTFormatContext<'_>> for FormatStmt<'_> {
|
|||
self.item,
|
||||
module.as_ref().map(String::as_str),
|
||||
names,
|
||||
level.as_ref(),
|
||||
*level,
|
||||
),
|
||||
StmtKind::Expr { value } => format_expr(f, self.item, value),
|
||||
}?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue