cargo clippy --fix

This commit is contained in:
Lukas Wirth 2025-07-31 10:30:22 +02:00
parent 423c7dd23a
commit 8ce30264c8
186 changed files with 3056 additions and 3314 deletions

View file

@ -610,18 +610,16 @@ impl<'db> NameClass<'db> {
let local = sema.to_def(&ident_pat)?;
let pat_parent = ident_pat.syntax().parent();
if let Some(record_pat_field) = pat_parent.and_then(ast::RecordPatField::cast) {
if record_pat_field.name_ref().is_none() {
if let Some((field, _, adt_subst)) =
sema.resolve_record_pat_field_with_subst(&record_pat_field)
{
return Some(NameClass::PatFieldShorthand {
local_def: local,
field_ref: field,
adt_subst,
});
}
}
if let Some(record_pat_field) = pat_parent.and_then(ast::RecordPatField::cast)
&& record_pat_field.name_ref().is_none()
&& let Some((field, _, adt_subst)) =
sema.resolve_record_pat_field_with_subst(&record_pat_field)
{
return Some(NameClass::PatFieldShorthand {
local_def: local,
field_ref: field,
adt_subst,
});
}
Some(NameClass::Definition(Definition::Local(local)))
}
@ -755,30 +753,27 @@ impl<'db> NameRefClass<'db> {
let parent = name_ref.syntax().parent()?;
if let Some(record_field) = ast::RecordExprField::for_field_name(name_ref) {
if let Some((field, local, _, adt_subst)) =
if let Some(record_field) = ast::RecordExprField::for_field_name(name_ref)
&& let Some((field, local, _, adt_subst)) =
sema.resolve_record_field_with_substitution(&record_field)
{
let res = match local {
None => NameRefClass::Definition(Definition::Field(field), Some(adt_subst)),
Some(local) => NameRefClass::FieldShorthand {
field_ref: field,
local_ref: local,
adt_subst,
},
};
return Some(res);
}
{
let res = match local {
None => NameRefClass::Definition(Definition::Field(field), Some(adt_subst)),
Some(local) => {
NameRefClass::FieldShorthand { field_ref: field, local_ref: local, adt_subst }
}
};
return Some(res);
}
if let Some(path) = ast::PathSegment::cast(parent.clone()).map(|it| it.parent_path()) {
if path.parent_path().is_none() {
if let Some(macro_call) = path.syntax().parent().and_then(ast::MacroCall::cast) {
// Only use this to resolve to macro calls for last segments as qualifiers resolve
// to modules below.
if let Some(macro_def) = sema.resolve_macro_call(&macro_call) {
return Some(NameRefClass::Definition(Definition::Macro(macro_def), None));
}
if path.parent_path().is_none()
&& let Some(macro_call) = path.syntax().parent().and_then(ast::MacroCall::cast)
{
// Only use this to resolve to macro calls for last segments as qualifiers resolve
// to modules below.
if let Some(macro_def) = sema.resolve_macro_call(&macro_call) {
return Some(NameRefClass::Definition(Definition::Macro(macro_def), None));
}
}
return sema
@ -820,8 +815,8 @@ impl<'db> NameRefClass<'db> {
// ^^^^^
let containing_path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
let resolved = sema.resolve_path(&containing_path)?;
if let PathResolution::Def(ModuleDef::Trait(tr)) = resolved {
if let Some(ty) = tr
if let PathResolution::Def(ModuleDef::Trait(tr)) = resolved
&& let Some(ty) = tr
.items_with_supertraits(sema.db)
.iter()
.filter_map(|&assoc| match assoc {
@ -833,7 +828,6 @@ impl<'db> NameRefClass<'db> {
// No substitution, this can only occur in type position.
return Some(NameRefClass::Definition(Definition::TypeAlias(ty), None));
}
}
None
},
ast::UseBoundGenericArgs(_) => {

View file

@ -70,11 +70,11 @@ pub fn visit_file_defs(
};
let mut defs: VecDeque<_> = module.declarations(db).into();
while let Some(def) = defs.pop_front() {
if let ModuleDef::Module(submodule) = def {
if submodule.is_inline(db) {
defs.extend(submodule.declarations(db));
submodule.impl_defs(db).into_iter().for_each(|impl_| cb(impl_.into()));
}
if let ModuleDef::Module(submodule) = def
&& submodule.is_inline(db)
{
defs.extend(submodule.declarations(db));
submodule.impl_defs(db).into_iter().for_each(|impl_| cb(impl_.into()));
}
cb(def.into());
}

View file

@ -97,12 +97,11 @@ impl ImportScope {
.map(ImportScopeKind::Module)
.map(|kind| ImportScope { kind, required_cfgs });
} else if let Some(has_attrs) = ast::AnyHasAttrs::cast(syntax) {
if block.is_none() {
if let Some(b) = ast::BlockExpr::cast(has_attrs.syntax().clone()) {
if let Some(b) = sema.original_ast_node(b) {
block = b.stmt_list();
}
}
if block.is_none()
&& let Some(b) = ast::BlockExpr::cast(has_attrs.syntax().clone())
&& let Some(b) = sema.original_ast_node(b)
{
block = b.stmt_list();
}
if has_attrs
.attrs()
@ -349,26 +348,24 @@ fn guess_granularity_from_scope(scope: &ImportScope) -> ImportGranularityGuess {
seen_one_style_groups.push((curr_vis.clone(), curr_attrs.clone()));
} else if eq_visibility(prev_vis, curr_vis.clone())
&& eq_attrs(prev_attrs, curr_attrs.clone())
&& let Some((prev_path, curr_path)) = prev.path().zip(curr.path())
&& let Some((prev_prefix, _)) = common_prefix(&prev_path, &curr_path)
{
if let Some((prev_path, curr_path)) = prev.path().zip(curr.path()) {
if let Some((prev_prefix, _)) = common_prefix(&prev_path, &curr_path) {
if prev.use_tree_list().is_none() && curr.use_tree_list().is_none() {
let prefix_c = prev_prefix.qualifiers().count();
let curr_c = curr_path.qualifiers().count() - prefix_c;
let prev_c = prev_path.qualifiers().count() - prefix_c;
if curr_c == 1 && prev_c == 1 {
// Same prefix, only differing in the last segment and no use tree lists so this has to be of item style.
break ImportGranularityGuess::Item;
} else {
// Same prefix and no use tree list but differs in more than one segment at the end. This might be module style still.
res = ImportGranularityGuess::ModuleOrItem;
}
} else {
// Same prefix with item tree lists, has to be module style as it
// can't be crate style since the trees wouldn't share a prefix then.
break ImportGranularityGuess::Module;
}
if prev.use_tree_list().is_none() && curr.use_tree_list().is_none() {
let prefix_c = prev_prefix.qualifiers().count();
let curr_c = curr_path.qualifiers().count() - prefix_c;
let prev_c = prev_path.qualifiers().count() - prefix_c;
if curr_c == 1 && prev_c == 1 {
// Same prefix, only differing in the last segment and no use tree lists so this has to be of item style.
break ImportGranularityGuess::Item;
} else {
// Same prefix and no use tree list but differs in more than one segment at the end. This might be module style still.
res = ImportGranularityGuess::ModuleOrItem;
}
} else {
// Same prefix with item tree lists, has to be module style as it
// can't be crate style since the trees wouldn't share a prefix then.
break ImportGranularityGuess::Module;
}
}
prev = curr;

View file

@ -193,13 +193,12 @@ impl<'a> PathTransform<'a> {
}
}
(Either::Right(k), None) => {
if let Some(default) = k.default(db) {
if let Some(default) =
if let Some(default) = k.default(db)
&& let Some(default) =
&default.display_source_code(db, source_module.into(), false).ok()
{
type_substs.insert(k, make::ty(default).clone_for_update());
defaulted_params.push(Either::Left(k));
}
{
type_substs.insert(k, make::ty(default).clone_for_update());
defaulted_params.push(Either::Left(k));
}
}
(Either::Left(k), Some(TypeOrConst::Either(v))) => {
@ -221,11 +220,10 @@ impl<'a> PathTransform<'a> {
(Either::Left(k), None) => {
if let Some(default) =
k.default(db, target_module.krate().to_display_target(db))
&& let Some(default) = default.expr()
{
if let Some(default) = default.expr() {
const_substs.insert(k, default.syntax().clone_for_update());
defaulted_params.push(Either::Right(k));
}
const_substs.insert(k, default.syntax().clone_for_update());
defaulted_params.push(Either::Right(k));
}
}
_ => (), // ignore mismatching params
@ -427,14 +425,14 @@ impl Ctx<'_> {
}
}
hir::PathResolution::Def(def) if def.as_assoc_item(self.source_scope.db).is_none() => {
if let hir::ModuleDef::Trait(_) = def {
if matches!(path.segment()?.kind()?, ast::PathSegmentKind::Type { .. }) {
// `speculative_resolve` resolves segments like `<T as
// Trait>` into `Trait`, but just the trait name should
// not be used as the replacement of the original
// segment.
return None;
}
if let hir::ModuleDef::Trait(_) = def
&& matches!(path.segment()?.kind()?, ast::PathSegmentKind::Type { .. })
{
// `speculative_resolve` resolves segments like `<T as
// Trait>` into `Trait`, but just the trait name should
// not be used as the replacement of the original
// segment.
return None;
}
let cfg = ImportPathConfig {
@ -446,19 +444,17 @@ impl Ctx<'_> {
let found_path = self.target_module.find_path(self.source_scope.db, def, cfg)?;
let res = mod_path_to_ast(&found_path, self.target_edition).clone_for_update();
let mut res_editor = SyntaxEditor::new(res.syntax().clone_subtree());
if let Some(args) = path.segment().and_then(|it| it.generic_arg_list()) {
if let Some(segment) = res.segment() {
if let Some(old) = segment.generic_arg_list() {
res_editor.replace(
old.syntax(),
args.clone_subtree().syntax().clone_for_update(),
)
} else {
res_editor.insert(
syntax_editor::Position::last_child_of(segment.syntax()),
args.clone_subtree().syntax().clone_for_update(),
);
}
if let Some(args) = path.segment().and_then(|it| it.generic_arg_list())
&& let Some(segment) = res.segment()
{
if let Some(old) = segment.generic_arg_list() {
res_editor
.replace(old.syntax(), args.clone_subtree().syntax().clone_for_update())
} else {
res_editor.insert(
syntax_editor::Position::last_child_of(segment.syntax()),
args.clone_subtree().syntax().clone_for_update(),
);
}
}
let res = res_editor.finish().new_root().clone();
@ -485,27 +481,27 @@ impl Ctx<'_> {
.ok()?;
let ast_ty = make::ty(ty_str).clone_for_update();
if let Some(adt) = ty.as_adt() {
if let ast::Type::PathType(path_ty) = &ast_ty {
let cfg = ImportPathConfig {
prefer_no_std: false,
prefer_prelude: true,
prefer_absolute: false,
allow_unstable: true,
};
let found_path = self.target_module.find_path(
self.source_scope.db,
ModuleDef::from(adt),
cfg,
)?;
if let Some(adt) = ty.as_adt()
&& let ast::Type::PathType(path_ty) = &ast_ty
{
let cfg = ImportPathConfig {
prefer_no_std: false,
prefer_prelude: true,
prefer_absolute: false,
allow_unstable: true,
};
let found_path = self.target_module.find_path(
self.source_scope.db,
ModuleDef::from(adt),
cfg,
)?;
if let Some(qual) =
mod_path_to_ast(&found_path, self.target_edition).qualifier()
{
let res = make::path_concat(qual, path_ty.path()?).clone_for_update();
editor.replace(path.syntax(), res.syntax());
return Some(());
}
if let Some(qual) =
mod_path_to_ast(&found_path, self.target_edition).qualifier()
{
let res = make::path_concat(qual, path_ty.path()?).clone_for_update();
editor.replace(path.syntax(), res.syntax());
return Some(());
}
}

View file

@ -442,17 +442,17 @@ fn source_edit_from_name(
name: &ast::Name,
new_name: &dyn Display,
) -> bool {
if ast::RecordPatField::for_field_name(name).is_some() {
if let Some(ident_pat) = name.syntax().parent().and_then(ast::IdentPat::cast) {
cov_mark::hit!(rename_record_pat_field_name_split);
// Foo { ref mut field } -> Foo { new_name: ref mut field }
// ^ insert `new_name: `
if ast::RecordPatField::for_field_name(name).is_some()
&& let Some(ident_pat) = name.syntax().parent().and_then(ast::IdentPat::cast)
{
cov_mark::hit!(rename_record_pat_field_name_split);
// Foo { ref mut field } -> Foo { new_name: ref mut field }
// ^ insert `new_name: `
// FIXME: instead of splitting the shorthand, recursively trigger a rename of the
// other name https://github.com/rust-lang/rust-analyzer/issues/6547
edit.insert(ident_pat.syntax().text_range().start(), format!("{new_name}: "));
return true;
}
// FIXME: instead of splitting the shorthand, recursively trigger a rename of the
// other name https://github.com/rust-lang/rust-analyzer/issues/6547
edit.insert(ident_pat.syntax().text_range().start(), format!("{new_name}: "));
return true;
}
false

View file

@ -295,10 +295,10 @@ impl Definition {
}
// def is crate root
if let &Definition::Module(module) = self {
if module.is_crate_root() {
return SearchScope::reverse_dependencies(db, module.krate());
}
if let &Definition::Module(module) = self
&& module.is_crate_root()
{
return SearchScope::reverse_dependencies(db, module.krate());
}
let module = match self.module(db) {
@ -683,51 +683,47 @@ impl<'a> FindUsages<'a> {
}
} else if let Some(alias) =
usage.ancestors().find_map(ast::TypeAlias::cast)
&& let Some(name) = alias.name()
&& seen
.insert(InFileWrapper::new(file_id, name.syntax().text_range()))
{
if let Some(name) = alias.name() {
if seen.insert(InFileWrapper::new(
file_id,
name.syntax().text_range(),
)) {
if let Some(def) = is_alias(&alias) {
cov_mark::hit!(container_type_alias);
insert_type_alias(
sema.db,
&mut to_process,
name.text().as_str(),
def.into(),
);
} else {
cov_mark::hit!(same_name_different_def_type_alias);
}
}
if let Some(def) = is_alias(&alias) {
cov_mark::hit!(container_type_alias);
insert_type_alias(
sema.db,
&mut to_process,
name.text().as_str(),
def.into(),
);
} else {
cov_mark::hit!(same_name_different_def_type_alias);
}
}
// We need to account for `Self`. It can only refer to our type inside an impl.
let impl_ = 'impl_: {
for ancestor in usage.ancestors() {
if let Some(parent) = ancestor.parent() {
if let Some(parent) = ast::Impl::cast(parent) {
// Only if the GENERIC_PARAM_LIST is directly under impl, otherwise it may be in the self ty.
if matches!(
ancestor.kind(),
SyntaxKind::ASSOC_ITEM_LIST
| SyntaxKind::WHERE_CLAUSE
| SyntaxKind::GENERIC_PARAM_LIST
) {
break;
}
if parent
.trait_()
.is_some_and(|trait_| *trait_.syntax() == ancestor)
{
break;
}
// Otherwise, found an impl where its self ty may be our type.
break 'impl_ Some(parent);
if let Some(parent) = ancestor.parent()
&& let Some(parent) = ast::Impl::cast(parent)
{
// Only if the GENERIC_PARAM_LIST is directly under impl, otherwise it may be in the self ty.
if matches!(
ancestor.kind(),
SyntaxKind::ASSOC_ITEM_LIST
| SyntaxKind::WHERE_CLAUSE
| SyntaxKind::GENERIC_PARAM_LIST
) {
break;
}
if parent
.trait_()
.is_some_and(|trait_| *trait_.syntax() == ancestor)
{
break;
}
// Otherwise, found an impl where its self ty may be our type.
break 'impl_ Some(parent);
}
}
None
@ -1356,11 +1352,10 @@ impl ReferenceCategory {
if matches!(expr.op_kind()?, ast::BinaryOp::Assignment { .. }) {
// If the variable or field ends on the LHS's end then it's a Write
// (covers fields and locals). FIXME: This is not terribly accurate.
if let Some(lhs) = expr.lhs() {
if lhs.syntax().text_range().end() == r.syntax().text_range().end() {
if let Some(lhs) = expr.lhs()
&& lhs.syntax().text_range().end() == r.syntax().text_range().end() {
return Some(ReferenceCategory::WRITE)
}
}
}
Some(ReferenceCategory::READ)
},

View file

@ -252,10 +252,10 @@ impl SymbolIndex {
let mut last_batch_start = 0;
for idx in 0..symbols.len() {
if let Some(next_symbol) = symbols.get(idx + 1) {
if cmp(&symbols[last_batch_start], next_symbol) == Ordering::Equal {
continue;
}
if let Some(next_symbol) = symbols.get(idx + 1)
&& cmp(&symbols[last_batch_start], next_symbol) == Ordering::Equal
{
continue;
}
let start = last_batch_start;
@ -371,10 +371,10 @@ impl Query {
if self.exclude_imports && symbol.is_import {
continue;
}
if self.mode.check(&self.query, self.case_sensitive, symbol_name) {
if let Some(b) = cb(symbol).break_value() {
return Some(b);
}
if self.mode.check(&self.query, self.case_sensitive, symbol_name)
&& let Some(b) = cb(symbol).break_value()
{
return Some(b);
}
}
}

View file

@ -230,11 +230,11 @@ pub fn lex_format_specifiers(
skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback);
}
continue;
} else if let '}' = first_char {
if let Some((_, '}')) = chars.peek() {
// Escaped format specifier, `}}`
read_escaped_format_specifier(&mut chars, &mut callback);
}
} else if let '}' = first_char
&& let Some((_, '}')) = chars.peek()
{
// Escaped format specifier, `}}`
read_escaped_format_specifier(&mut chars, &mut callback);
}
}

View file

@ -79,14 +79,13 @@ pub fn preorder_expr_with_ctx_checker(
continue;
}
};
if let Some(let_stmt) = node.parent().and_then(ast::LetStmt::cast) {
if let_stmt.initializer().map(|it| it.syntax() != &node).unwrap_or(true)
&& let_stmt.let_else().map(|it| it.syntax() != &node).unwrap_or(true)
{
// skipping potential const pat expressions in let statements
preorder.skip_subtree();
continue;
}
if let Some(let_stmt) = node.parent().and_then(ast::LetStmt::cast)
&& let_stmt.initializer().map(|it| it.syntax() != &node).unwrap_or(true)
&& let_stmt.let_else().map(|it| it.syntax() != &node).unwrap_or(true)
{
// skipping potential const pat expressions in let statements
preorder.skip_subtree();
continue;
}
match ast::Stmt::cast(node.clone()) {
@ -306,10 +305,10 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) {
Some(ast::BlockModifier::AsyncGen(_)) => (),
None => (),
}
if let Some(stmt_list) = b.stmt_list() {
if let Some(e) = stmt_list.tail_expr() {
for_each_tail_expr(&e, cb);
}
if let Some(stmt_list) = b.stmt_list()
&& let Some(e) = stmt_list.tail_expr()
{
for_each_tail_expr(&e, cb);
}
}
ast::Expr::IfExpr(if_) => {

View file

@ -16,17 +16,17 @@ pub fn use_trivial_constructor(
) -> Option<Expr> {
match ty.as_adt() {
Some(hir::Adt::Enum(x)) => {
if let &[variant] = &*x.variants(db) {
if variant.kind(db) == hir::StructKind::Unit {
let path = make::path_qualified(
path,
make::path_segment(make::name_ref(
&variant.name(db).display_no_db(edition).to_smolstr(),
)),
);
if let &[variant] = &*x.variants(db)
&& variant.kind(db) == hir::StructKind::Unit
{
let path = make::path_qualified(
path,
make::path_segment(make::name_ref(
&variant.name(db).display_no_db(edition).to_smolstr(),
)),
);
return Some(make::expr_path(path));
}
return Some(make::expr_path(path));
}
}
Some(hir::Adt::Struct(x)) if x.kind(db) == StructKind::Unit => {