Remove `unwrap` from none-comparison rule (#4969)

This commit is contained in:
Charlie Marsh 2023-06-08 14:21:56 -04:00 committed by GitHub
parent 775d247731
commit d042eddccc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 111 additions and 94 deletions

View File

@ -16,12 +16,12 @@ enum EqCmpop {
NotEq, NotEq,
} }
impl From<&Cmpop> for EqCmpop { impl EqCmpop {
fn from(cmpop: &Cmpop) -> Self { fn try_from(value: Cmpop) -> Option<EqCmpop> {
match cmpop { match value {
Cmpop::Eq => EqCmpop::Eq, Cmpop::Eq => Some(EqCmpop::Eq),
Cmpop::NotEq => EqCmpop::NotEq, Cmpop::NotEq => Some(EqCmpop::NotEq),
_ => panic!("Expected Cmpop::Eq | Cmpop::NotEq"), _ => None,
} }
} }
} }
@ -154,54 +154,64 @@ pub(crate) fn literal_comparisons(
let next = &comparators[0]; let next = &comparators[0];
if !helpers::is_constant_non_singleton(next) { if !helpers::is_constant_non_singleton(next) {
if check_none_comparisons if let Some(op) = EqCmpop::try_from(*op) {
&& matches!( if check_none_comparisons
comparator, && matches!(
Expr::Constant(ast::ExprConstant { comparator,
value: Constant::None, Expr::Constant(ast::ExprConstant {
kind: None, value: Constant::None,
range: _ kind: None,
}) range: _
) })
{ )
if matches!(op, Cmpop::Eq) {
let diagnostic = Diagnostic::new(NoneComparison(op.into()), comparator.range());
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(0, Cmpop::Is);
}
diagnostics.push(diagnostic);
}
if matches!(op, Cmpop::NotEq) {
let diagnostic = Diagnostic::new(NoneComparison(op.into()), comparator.range());
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(0, Cmpop::IsNot);
}
diagnostics.push(diagnostic);
}
}
if check_true_false_comparisons {
if let Expr::Constant(ast::ExprConstant {
value: Constant::Bool(value),
kind: None,
range: _,
}) = comparator
{ {
if matches!(op, Cmpop::Eq) { match op {
let diagnostic = EqCmpop::Eq => {
Diagnostic::new(TrueFalseComparison(*value, op.into()), comparator.range()); let diagnostic = Diagnostic::new(NoneComparison(op), comparator.range());
if checker.patch(diagnostic.kind.rule()) { if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(0, Cmpop::Is); bad_ops.insert(0, Cmpop::Is);
}
diagnostics.push(diagnostic);
}
EqCmpop::NotEq => {
let diagnostic = Diagnostic::new(NoneComparison(op), comparator.range());
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(0, Cmpop::IsNot);
}
diagnostics.push(diagnostic);
} }
diagnostics.push(diagnostic);
} }
if matches!(op, Cmpop::NotEq) { }
let diagnostic =
Diagnostic::new(TrueFalseComparison(*value, op.into()), comparator.range()); if check_true_false_comparisons {
if checker.patch(diagnostic.kind.rule()) { if let Expr::Constant(ast::ExprConstant {
bad_ops.insert(0, Cmpop::IsNot); value: Constant::Bool(value),
kind: None,
range: _,
}) = comparator
{
match op {
EqCmpop::Eq => {
let diagnostic = Diagnostic::new(
TrueFalseComparison(*value, op),
comparator.range(),
);
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(0, Cmpop::Is);
}
diagnostics.push(diagnostic);
}
EqCmpop::NotEq => {
let diagnostic = Diagnostic::new(
TrueFalseComparison(*value, op),
comparator.range(),
);
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(0, Cmpop::IsNot);
}
diagnostics.push(diagnostic);
}
} }
diagnostics.push(diagnostic);
} }
} }
} }
@ -214,53 +224,60 @@ pub(crate) fn literal_comparisons(
continue; continue;
} }
if check_none_comparisons if let Some(op) = EqCmpop::try_from(*op) {
&& matches!( if check_none_comparisons
next, && matches!(
Expr::Constant(ast::ExprConstant { next,
value: Constant::None, Expr::Constant(ast::ExprConstant {
kind: None, value: Constant::None,
range: _ kind: None,
}) range: _
) })
{ )
if matches!(op, Cmpop::Eq) {
let diagnostic = Diagnostic::new(NoneComparison(op.into()), next.range());
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(idx, Cmpop::Is);
}
diagnostics.push(diagnostic);
}
if matches!(op, Cmpop::NotEq) {
let diagnostic = Diagnostic::new(NoneComparison(op.into()), next.range());
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(idx, Cmpop::IsNot);
}
diagnostics.push(diagnostic);
}
}
if check_true_false_comparisons {
if let Expr::Constant(ast::ExprConstant {
value: Constant::Bool(value),
kind: None,
range: _,
}) = next
{ {
if op.is_eq() { match op {
let diagnostic = EqCmpop::Eq => {
Diagnostic::new(TrueFalseComparison(*value, op.into()), next.range()); let diagnostic = Diagnostic::new(NoneComparison(op), next.range());
if checker.patch(diagnostic.kind.rule()) { if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(idx, Cmpop::Is); bad_ops.insert(idx, Cmpop::Is);
}
diagnostics.push(diagnostic);
} }
diagnostics.push(diagnostic); EqCmpop::NotEq => {
} else if op.is_not_eq() { let diagnostic = Diagnostic::new(NoneComparison(op), next.range());
let diagnostic = if checker.patch(diagnostic.kind.rule()) {
Diagnostic::new(TrueFalseComparison(*value, op.into()), next.range()); bad_ops.insert(idx, Cmpop::IsNot);
if checker.patch(diagnostic.kind.rule()) { }
bad_ops.insert(idx, Cmpop::IsNot); diagnostics.push(diagnostic);
}
}
}
if check_true_false_comparisons {
if let Expr::Constant(ast::ExprConstant {
value: Constant::Bool(value),
kind: None,
range: _,
}) = next
{
match op {
EqCmpop::Eq => {
let diagnostic =
Diagnostic::new(TrueFalseComparison(*value, op), next.range());
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(idx, Cmpop::Is);
}
diagnostics.push(diagnostic);
}
EqCmpop::NotEq => {
let diagnostic =
Diagnostic::new(TrueFalseComparison(*value, op), next.range());
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(idx, Cmpop::IsNot);
}
diagnostics.push(diagnostic);
}
} }
diagnostics.push(diagnostic);
} }
} }
} }