Skip to content

Commit

Permalink
minor: New clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jan 6, 2025
1 parent 7393621 commit 0b832bf
Show file tree
Hide file tree
Showing 92 changed files with 180 additions and 201 deletions.
5 changes: 5 additions & 0 deletions src/tools/rust-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ useless_asref = "allow"
assigning_clones = "allow"
# Does not work with macros
vec_init_then_push = "allow"
# Our tests have a lot of these
literal_string_with_formatting_args = "allow"
# This lint has been empowered but now also triggers on cases where its invalid to do so
# due to it ignoring move analysis
unnecessary_map_or = "allow"

## Following lints should be tackled at some point
too_many_arguments = "allow"
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/hir-def/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ impl<'attr> AttrQuery<'attr> {

pub fn attrs(self) -> impl Iterator<Item = &'attr Attr> + Clone {
let key = self.key;
self.attrs.iter().filter(move |attr| attr.path.as_ident().map_or(false, |s| *s == *key))
self.attrs.iter().filter(move |attr| attr.path.as_ident().is_some_and(|s| *s == *key))
}

/// Find string value for a specific key inside token tree
Expand Down
12 changes: 6 additions & 6 deletions src/tools/rust-analyzer/crates/hir-def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2216,11 +2216,11 @@ impl ExprCollector<'_> {
};
// This needs to match `Flag` in library/core/src/fmt/rt.rs.
let flags: u32 = ((sign == Some(FormatSign::Plus)) as u32)
| ((sign == Some(FormatSign::Minus)) as u32) << 1
| (alternate as u32) << 2
| (zero_pad as u32) << 3
| ((debug_hex == Some(FormatDebugHex::Lower)) as u32) << 4
| ((debug_hex == Some(FormatDebugHex::Upper)) as u32) << 5;
| (((sign == Some(FormatSign::Minus)) as u32) << 1)
| ((alternate as u32) << 2)
| ((zero_pad as u32) << 3)
| (((debug_hex == Some(FormatDebugHex::Lower)) as u32) << 4)
| (((debug_hex == Some(FormatDebugHex::Upper)) as u32) << 5);
let flags = self.alloc_expr_desugared(Expr::Literal(Literal::Uint(
flags as u128,
Some(BuiltinUint::U32),
Expand Down Expand Up @@ -2468,7 +2468,7 @@ impl ExprCollector<'_> {

fn comma_follows_token(t: Option<syntax::SyntaxToken>) -> bool {
(|| syntax::algo::skip_trivia_token(t?.next_token()?, syntax::Direction::Next))()
.map_or(false, |it| it.kind() == syntax::T![,])
.is_some_and(|it| it.kind() == syntax::T![,])
}

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
Expand Down
4 changes: 2 additions & 2 deletions src/tools/rust-analyzer/crates/hir-def/src/dyn_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub mod keys {
map.map.get::<FxHashMap<AstPtr<AST>, ID>>()?.get(key)
}
fn is_empty(map: &DynMap) -> bool {
map.map.get::<FxHashMap<AstPtr<AST>, ID>>().map_or(true, |it| it.is_empty())
map.map.get::<FxHashMap<AstPtr<AST>, ID>>().is_none_or(|it| it.is_empty())
}
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ impl<K: Hash + Eq + 'static, V: 'static> Policy for (K, V) {
map.map.get::<FxHashMap<K, V>>()?.get(key)
}
fn is_empty(map: &DynMap) -> bool {
map.map.get::<FxHashMap<K, V>>().map_or(true, |it| it.is_empty())
map.map.get::<FxHashMap<K, V>>().is_none_or(|it| it.is_empty())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ impl DefCollector<'_> {
let is_cfg_enabled = item_tree
.top_level_attrs(self.db, self.def_map.krate)
.cfg()
.map_or(true, |cfg| self.cfg_options.check(&cfg) != Some(false));
.is_none_or(|cfg| self.cfg_options.check(&cfg) != Some(false));
if is_cfg_enabled {
self.inject_prelude();

Expand Down
10 changes: 5 additions & 5 deletions src/tools/rust-analyzer/crates/hir-expand/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ impl RawAttrs {
.chain(b.slice.iter().map(|it| {
let mut it = it.clone();
it.id.id = (it.id.ast_index() as u32 + last_ast_index)
| (it.id.cfg_attr_index().unwrap_or(0) as u32)
<< AttrId::AST_INDEX_BITS;
| ((it.id.cfg_attr_index().unwrap_or(0) as u32)
<< AttrId::AST_INDEX_BITS);
it
}))
.collect::<Vec<_>>();
Expand All @@ -122,7 +122,7 @@ impl RawAttrs {
pub fn filter(self, db: &dyn ExpandDatabase, krate: CrateId) -> RawAttrs {
let has_cfg_attrs = self
.iter()
.any(|attr| attr.path.as_ident().map_or(false, |name| *name == sym::cfg_attr.clone()));
.any(|attr| attr.path.as_ident().is_some_and(|name| *name == sym::cfg_attr.clone()));
if !has_cfg_attrs {
return self;
}
Expand All @@ -132,7 +132,7 @@ impl RawAttrs {
self.iter()
.flat_map(|attr| -> SmallVec<[_; 1]> {
let is_cfg_attr =
attr.path.as_ident().map_or(false, |name| *name == sym::cfg_attr.clone());
attr.path.as_ident().is_some_and(|name| *name == sym::cfg_attr.clone());
if !is_cfg_attr {
return smallvec![attr.clone()];
}
Expand Down Expand Up @@ -202,7 +202,7 @@ impl AttrId {
}

pub fn with_cfg_attr(self, idx: usize) -> AttrId {
AttrId { id: self.id | (idx as u32) << Self::AST_INDEX_BITS | Self::CFG_ATTR_SET_BITS }
AttrId { id: self.id | ((idx as u32) << Self::AST_INDEX_BITS) | Self::CFG_ATTR_SET_BITS }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/hir-expand/src/fixup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub(crate) fn fixup_syntax(
}
},
ast::ExprStmt(it) => {
let needs_semi = it.semicolon_token().is_none() && it.expr().map_or(false, |e| e.syntax().kind() != SyntaxKind::BLOCK_EXPR);
let needs_semi = it.semicolon_token().is_none() && it.expr().is_some_and(|e| e.syntax().kind() != SyntaxKind::BLOCK_EXPR);
if needs_semi {
append.insert(node.clone().into(), vec![
Leaf::Punct(Punct {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ impl ExpandTo {
if parent.kind() == MACRO_EXPR
&& parent
.parent()
.map_or(false, |p| matches!(p.kind(), EXPR_STMT | STMT_LIST | MACRO_STMTS))
.is_some_and(|p| matches!(p.kind(), EXPR_STMT | STMT_LIST | MACRO_STMTS))
{
return ExpandTo::Statements;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn is_camel_case(name: &str) -> bool {
let mut fst = None;
// start with a non-lowercase letter rather than non-uppercase
// ones (some scripts don't have a concept of upper/lowercase)
name.chars().next().map_or(true, |c| !c.is_lowercase())
name.chars().next().is_none_or(|c| !c.is_lowercase())
&& !name.contains("__")
&& !name.chars().any(|snd| {
let ret = match fst {
Expand Down
6 changes: 3 additions & 3 deletions src/tools/rust-analyzer/crates/hir-ty/src/diagnostics/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ impl ExprValidator {
path,
self.body.expr_path_hygiene(scrutinee_expr),
);
value_or_partial.map_or(true, |v| !matches!(v, ValueNs::StaticId(_)))
value_or_partial.is_none_or(|v| !matches!(v, ValueNs::StaticId(_)))
}
Expr::Field { expr, .. } => match self.infer.type_of_expr[*expr].kind(Interner) {
TyKind::Adt(adt, ..)
Expand Down Expand Up @@ -447,7 +447,7 @@ impl ExprValidator {
loop {
let parent = top_if_expr.syntax().parent();
let has_parent_expr_stmt_or_stmt_list =
parent.as_ref().map_or(false, |node| {
parent.as_ref().is_some_and(|node| {
ast::ExprStmt::can_cast(node.kind())
| ast::StmtList::can_cast(node.kind())
});
Expand Down Expand Up @@ -529,7 +529,7 @@ impl FilterMapNextChecker {
let is_dyn_trait = self
.prev_receiver_ty
.as_ref()
.map_or(false, |it| it.strip_references().dyn_trait().is_some());
.is_some_and(|it| it.strip_references().dyn_trait().is_some());
if *receiver_expr_id == prev_filter_map_expr_id && !is_dyn_trait {
return Some(());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ impl<'db> MatchCheckCtx<'db> {
}
}

impl<'db> PatCx for MatchCheckCtx<'db> {
impl PatCx for MatchCheckCtx<'_> {
type Error = ();
type Ty = Ty;
type VariantIdx = EnumVariantContiguousIndex;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/hir-ty/src/infer/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ impl InferenceContext<'_> {
},
},
}
if self.result.pat_adjustments.get(&p).map_or(false, |it| !it.is_empty()) {
if self.result.pat_adjustments.get(&p).is_some_and(|it| !it.is_empty()) {
for_mut = BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture };
}
self.body.walk_pats_shallow(p, |p| self.walk_pat_inner(p, update_result, for_mut));
Expand Down
6 changes: 3 additions & 3 deletions src/tools/rust-analyzer/crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl InferenceContext<'_> {
path,
self.body.expr_path_hygiene(expr),
)
.map_or(true, |res| matches!(res, ValueNs::LocalBinding(_) | ValueNs::StaticId(_))),
.is_none_or(|res| matches!(res, ValueNs::LocalBinding(_) | ValueNs::StaticId(_))),
Expr::Underscore => true,
Expr::UnaryOp { op: UnaryOp::Deref, .. } => true,
Expr::Field { .. } | Expr::Index { .. } => true,
Expand Down Expand Up @@ -499,7 +499,7 @@ impl InferenceContext<'_> {
// if the function is unresolved, we use is_varargs=true to
// suppress the arg count diagnostic here
let is_varargs =
derefed_callee.callable_sig(self.db).map_or(false, |sig| sig.is_varargs)
derefed_callee.callable_sig(self.db).is_some_and(|sig| sig.is_varargs)
|| res.is_none();
let (param_tys, ret_ty) = match res {
Some((func, params, ret_ty)) => {
Expand Down Expand Up @@ -2043,7 +2043,7 @@ impl InferenceContext<'_> {
continue;
}

while skip_indices.peek().map_or(false, |i| *i < idx as u32) {
while skip_indices.peek().is_some_and(|i| *i < idx as u32) {
skip_indices.next();
}
if skip_indices.peek().copied() == Some(idx as u32) {
Expand Down
8 changes: 1 addition & 7 deletions src/tools/rust-analyzer/crates/hir-ty/src/infer/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,7 @@ impl InferenceContext<'_> {
}

AssocItemId::ConstId(konst) => {
if self
.db
.const_data(konst)
.name
.as_ref()
.map_or(false, |n| n == segment.name)
{
if self.db.const_data(konst).name.as_ref() == Some(segment.name) {
Some(AssocItemId::ConstId(konst))
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/hir-ty/src/infer/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ impl<'a> InferenceTable<'a> {
let is_diverging = self
.type_variable_table
.get(iv.index() as usize)
.map_or(false, |data| data.contains(TypeVariableFlags::DIVERGING));
.is_some_and(|data| data.contains(TypeVariableFlags::DIVERGING));
if is_diverging {
return TyKind::Never.intern(Interner);
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/hir-ty/src/inhabitedness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl UninhabitedFrom<'_> {
ty: &Binders<Ty>,
subst: &Substitution,
) -> ControlFlow<VisiblyUninhabited> {
if vis.map_or(true, |it| it.is_visible_from(self.db.upcast(), self.target_mod)) {
if vis.is_none_or(|it| it.is_visible_from(self.db.upcast(), self.target_mod)) {
let ty = ty.clone().substitute(Interner, subst);
ty.visit_with(self, DebruijnIndex::INNERMOST)
} else {
Expand Down
14 changes: 7 additions & 7 deletions src/tools/rust-analyzer/crates/hir-ty/src/layout/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,31 +241,31 @@ fn recursive() {
#[test]
fn repr_packed() {
size_and_align! {
#[repr(packed)]
#[repr(Rust, packed)]
struct Goal;
}
size_and_align! {
#[repr(packed(2))]
#[repr(Rust, packed(2))]
struct Goal;
}
size_and_align! {
#[repr(packed(4))]
#[repr(Rust, packed(4))]
struct Goal;
}
size_and_align! {
#[repr(packed)]
#[repr(Rust, packed)]
struct Goal(i32);
}
size_and_align! {
#[repr(packed(2))]
#[repr(Rust, packed(2))]
struct Goal(i32);
}
size_and_align! {
#[repr(packed(4))]
#[repr(Rust, packed(4))]
struct Goal(i32);
}

check_size_and_align("#[repr(packed(5))] struct Goal(i32);", "", 4, 1);
check_size_and_align("#[repr(Rust, packed(5))] struct Goal(i32);", "", 4, 1);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/tools/rust-analyzer/crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2751,14 +2751,14 @@ fn fallback_bound_vars<T: TypeFoldable<Interner> + HasInterner<Interner = Intern
crate::fold_free_vars(
s,
|bound, binders| {
if bound.index_if_innermost().map_or(true, is_allowed) {
if bound.index_if_innermost().is_none_or(is_allowed) {
bound.shifted_in_from(binders).to_ty(Interner)
} else {
TyKind::Error.intern(Interner)
}
},
|ty, bound, binders| {
if bound.index_if_innermost().map_or(true, is_allowed) {
if bound.index_if_innermost().is_none_or(is_allowed) {
bound.shifted_in_from(binders).to_const(Interner, ty)
} else {
unknown_const(ty)
Expand Down
16 changes: 8 additions & 8 deletions src/tools/rust-analyzer/crates/hir-ty/src/method_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ pub(crate) fn incoherent_inherent_impl_crates(

for krate in crate_graph.transitive_deps(krate) {
let impls = db.inherent_impls_in_crate(krate);
if impls.map.get(&fp).map_or(false, |v| !v.is_empty()) {
if impls.map.get(&fp).is_some_and(|v| !v.is_empty()) {
res.push(krate);
}
}
Expand Down Expand Up @@ -811,7 +811,7 @@ fn is_inherent_impl_coherent(
| TyKind::Scalar(_) => def_map.is_rustc_coherence_is_core(),

&TyKind::Adt(AdtId(adt), _) => adt.module(db.upcast()).krate() == def_map.krate(),
TyKind::Dyn(it) => it.principal_id().map_or(false, |trait_id| {
TyKind::Dyn(it) => it.principal_id().is_some_and(|trait_id| {
from_chalk_trait_id(trait_id).module(db.upcast()).krate() == def_map.krate()
}),

Expand Down Expand Up @@ -840,7 +840,7 @@ fn is_inherent_impl_coherent(
.contains(StructFlags::IS_RUSTC_HAS_INCOHERENT_INHERENT_IMPL),
hir_def::AdtId::EnumId(it) => db.enum_data(it).rustc_has_incoherent_inherent_impls,
},
TyKind::Dyn(it) => it.principal_id().map_or(false, |trait_id| {
TyKind::Dyn(it) => it.principal_id().is_some_and(|trait_id| {
db.trait_data(from_chalk_trait_id(trait_id))
.flags
.contains(TraitFlags::RUSTC_HAS_INCOHERENT_INHERENT_IMPLS)
Expand Down Expand Up @@ -903,7 +903,7 @@ pub fn check_orphan_rules(db: &dyn HirDatabase, impl_: ImplId) -> bool {
match unwrap_fundamental(ty).kind(Interner) {
&TyKind::Adt(AdtId(id), _) => is_local(id.module(db.upcast()).krate()),
TyKind::Error => true,
TyKind::Dyn(it) => it.principal_id().map_or(false, |trait_id| {
TyKind::Dyn(it) => it.principal_id().is_some_and(|trait_id| {
is_local(from_chalk_trait_id(trait_id).module(db.upcast()).krate())
}),
_ => false,
Expand Down Expand Up @@ -1481,7 +1481,7 @@ fn is_valid_impl_method_candidate(
AssocItemId::ConstId(c) => {
let db = table.db;
check_that!(receiver_ty.is_none());
check_that!(name.map_or(true, |n| db.const_data(c).name.as_ref() == Some(n)));
check_that!(name.is_none_or(|n| db.const_data(c).name.as_ref() == Some(n)));

if let Some(from_module) = visible_from_module {
if !db.const_visibility(c).is_visible_from(db.upcast(), from_module) {
Expand Down Expand Up @@ -1519,7 +1519,7 @@ fn is_valid_trait_method_candidate(
AssocItemId::FunctionId(fn_id) => {
let data = db.function_data(fn_id);

check_that!(name.map_or(true, |n| n == &data.name));
check_that!(name.is_none_or(|n| n == &data.name));

table.run_in_snapshot(|table| {
let impl_subst = TyBuilder::subst_for_def(db, trait_id, None)
Expand Down Expand Up @@ -1548,7 +1548,7 @@ fn is_valid_trait_method_candidate(
}
AssocItemId::ConstId(c) => {
check_that!(receiver_ty.is_none());
check_that!(name.map_or(true, |n| db.const_data(c).name.as_ref() == Some(n)));
check_that!(name.is_none_or(|n| db.const_data(c).name.as_ref() == Some(n)));

IsValidCandidate::Yes
}
Expand All @@ -1569,7 +1569,7 @@ fn is_valid_impl_fn_candidate(
let db = table.db;
let data = db.function_data(fn_id);

check_that!(name.map_or(true, |n| n == &data.name));
check_that!(name.is_none_or(|n| n == &data.name));
if let Some(from_module) = visible_from_module {
if !db.function_visibility(fn_id).is_visible_from(db.upcast(), from_module) {
cov_mark::hit!(autoderef_candidate_not_visible);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/hir-ty/src/mir/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2113,7 +2113,7 @@ impl Evaluator<'_> {
while self.heap.len() % align != 0 {
self.heap.push(0);
}
if size.checked_add(self.heap.len()).map_or(true, |x| x > self.memory_limit) {
if size.checked_add(self.heap.len()).is_none_or(|x| x > self.memory_limit) {
return Err(MirEvalError::Panic(format!("Memory allocation of {size} bytes failed")));
}
let pos = self.heap.len();
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/hir-ty/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ impl OpaqueInternableThing for InTypeConstIdMetadata {
}

fn dyn_eq(&self, other: &dyn OpaqueInternableThing) -> bool {
other.as_any().downcast_ref::<Self>().map_or(false, |x| self == x)
other.as_any().downcast_ref::<Self>() == Some(self)
}

fn dyn_clone(&self) -> Box<dyn OpaqueInternableThing> {
Expand Down
Loading

0 comments on commit 0b832bf

Please sign in to comment.