Skip to content

Commit

Permalink
add some style improvement for [ifs_same_cond]
Browse files Browse the repository at this point in the history
  • Loading branch information
J-ZhengLi committed Mar 6, 2023
1 parent b180be9 commit 396ac3c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
33 changes: 13 additions & 20 deletions clippy_lints/src/copies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,14 @@ declare_clippy_lint! {

pub struct CopyAndPaste {
ignore_interior_mutability: Vec<String>,
ignored_ty_ids: FxHashSet<DefId>,
}

impl CopyAndPaste {
pub fn new(ignore_interior_mutability: Vec<String>) -> Self {
Self {
ignore_interior_mutability,
ignored_ty_ids: FxHashSet::default(),
}
}
}
Expand All @@ -182,17 +184,18 @@ impl_lint_pass!(CopyAndPaste => [
]);

impl<'tcx> LateLintPass<'tcx> for CopyAndPaste {
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
for ignored_ty in &self.ignore_interior_mutability {
let path: Vec<&str> = ignored_ty.split("::").collect();
for id in def_path_def_ids(cx, path.as_slice()) {
self.ignored_ty_ids.insert(id);
}
}
}
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if !expr.span.from_expansion() && matches!(expr.kind, ExprKind::If(..)) && !is_else_clause(cx.tcx, expr) {
let (conds, blocks) = if_sequence(expr);
let mut ignored_ty_ids = FxHashSet::default();
for ignored_ty in &self.ignore_interior_mutability {
let path: Vec<&str> = ignored_ty.split("::").collect();
for id in def_path_def_ids(cx, path.as_slice()) {
ignored_ty_ids.insert(id);
}
}
lint_same_cond(cx, &conds, &ignored_ty_ids);
lint_same_cond(cx, &conds, &self.ignored_ty_ids);
lint_same_fns_in_if_cond(cx, &conds);
let all_same =
!is_lint_allowed(cx, IF_SAME_THEN_ELSE, expr.hir_id) && lint_if_same_then_else(cx, &conds, &blocks);
Expand Down Expand Up @@ -575,22 +578,12 @@ fn method_caller_is_ignored_or_mutable(
ignored_ty_ids: &FxHashSet<DefId>,
) -> bool {
let caller_ty = cx.typeck_results().expr_ty(caller_expr);
let is_ignored_ty = if let Some(adt_id) = caller_ty.ty_adt_id() && ignored_ty_ids.contains(&adt_id) {
true
} else {
false
};

if is_ignored_ty
matches!(caller_ty.ty_adt_id(), Some(adt_id) if ignored_ty_ids.contains(&adt_id))
|| caller_ty.is_mutable_ptr()
// Check if a binding is mutable or not
|| path_to_local(caller_expr)
.and_then(|hid| find_binding_init(cx, hid))
.is_none()
{
return true;
}

false
}

/// Implementation of `IFS_SAME_COND`.
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/utils/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ define_Conf! {
///
/// A list of paths to types that should be treated like `Arc`, i.e. ignored but
/// for the generic parameters for determining interior mutability
(ignore_interior_mutability: Vec<String> = Vec::from(["bytes::Bytes".into()])),
(ignore_interior_mutability: Vec<String> = Vec::from(["bytes::Bytes".into(), "std::sync::Arc".into()])),
/// Lint: UNINLINED_FORMAT_ARGS.
///
/// Whether to allow mixed uninlined format args, e.g. `format!("{} {}", a, foo.bar)`
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-toml/ifs_same_cond/clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ignore-interior-mutability = ["std::cell::Cell"]
ignore-interior-mutability = ["std::cell::Cell"]

0 comments on commit 396ac3c

Please sign in to comment.