Skip to content

Commit bf199a9

Browse files
committed
add some style improvement for [ifs_same_cond];
add `Arc`, `Mutex`, `Cell`, `RefCell`, `RWLock` as `ignore-interior-mutability` default config
1 parent b180be9 commit bf199a9

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

clippy_lints/src/copies.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,14 @@ declare_clippy_lint! {
164164

165165
pub struct CopyAndPaste {
166166
ignore_interior_mutability: Vec<String>,
167+
ignored_ty_ids: FxHashSet<DefId>,
167168
}
168169

169170
impl CopyAndPaste {
170171
pub fn new(ignore_interior_mutability: Vec<String>) -> Self {
171172
Self {
172173
ignore_interior_mutability,
174+
ignored_ty_ids: FxHashSet::default(),
173175
}
174176
}
175177
}
@@ -182,17 +184,18 @@ impl_lint_pass!(CopyAndPaste => [
182184
]);
183185

184186
impl<'tcx> LateLintPass<'tcx> for CopyAndPaste {
187+
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
188+
for ignored_ty in &self.ignore_interior_mutability {
189+
let path: Vec<&str> = ignored_ty.split("::").collect();
190+
for id in def_path_def_ids(cx, path.as_slice()) {
191+
self.ignored_ty_ids.insert(id);
192+
}
193+
}
194+
}
185195
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
186196
if !expr.span.from_expansion() && matches!(expr.kind, ExprKind::If(..)) && !is_else_clause(cx.tcx, expr) {
187197
let (conds, blocks) = if_sequence(expr);
188-
let mut ignored_ty_ids = FxHashSet::default();
189-
for ignored_ty in &self.ignore_interior_mutability {
190-
let path: Vec<&str> = ignored_ty.split("::").collect();
191-
for id in def_path_def_ids(cx, path.as_slice()) {
192-
ignored_ty_ids.insert(id);
193-
}
194-
}
195-
lint_same_cond(cx, &conds, &ignored_ty_ids);
198+
lint_same_cond(cx, &conds, &self.ignored_ty_ids);
196199
lint_same_fns_in_if_cond(cx, &conds);
197200
let all_same =
198201
!is_lint_allowed(cx, IF_SAME_THEN_ELSE, expr.hir_id) && lint_if_same_then_else(cx, &conds, &blocks);
@@ -575,22 +578,12 @@ fn method_caller_is_ignored_or_mutable(
575578
ignored_ty_ids: &FxHashSet<DefId>,
576579
) -> bool {
577580
let caller_ty = cx.typeck_results().expr_ty(caller_expr);
578-
let is_ignored_ty = if let Some(adt_id) = caller_ty.ty_adt_id() && ignored_ty_ids.contains(&adt_id) {
579-
true
580-
} else {
581-
false
582-
};
583-
584-
if is_ignored_ty
581+
matches!(caller_ty.ty_adt_id(), Some(adt_id) if ignored_ty_ids.contains(&adt_id))
585582
|| caller_ty.is_mutable_ptr()
583+
// Check if a binding is mutable or not
586584
|| path_to_local(caller_expr)
587585
.and_then(|hid| find_binding_init(cx, hid))
588586
.is_none()
589-
{
590-
return true;
591-
}
592-
593-
false
594587
}
595588

596589
/// Implementation of `IFS_SAME_COND`.

clippy_lints/src/utils/conf.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,13 @@ define_Conf! {
441441
///
442442
/// A list of paths to types that should be treated like `Arc`, i.e. ignored but
443443
/// for the generic parameters for determining interior mutability
444-
(ignore_interior_mutability: Vec<String> = Vec::from(["bytes::Bytes".into()])),
444+
(ignore_interior_mutability: Vec<String> = Vec::from([
445+
"bytes::Bytes".into(),
446+
"std::sync::Arc".into(),
447+
"std::sync::Mutex".into(),
448+
"std::cell::Cell".into(),
449+
"std::cell::RefCell".into(),
450+
])),
445451
/// Lint: UNINLINED_FORMAT_ARGS.
446452
///
447453
/// Whether to allow mixed uninlined format args, e.g. `format!("{} {}", a, foo.bar)`
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ignore-interior-mutability = ["std::cell::Cell"]
1+
ignore-interior-mutability = ["std::cell::Cell"]

0 commit comments

Comments
 (0)