Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid removing side effects for boolean simplifications #1984

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions resources/test/fixtures/flake8_simplify/SIM222.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
if a or (b or True): # SIM223
pass

if a and True:
if a and True: # OK
pass

if True:
if True: # OK
pass


def validate(self, value):
return json.loads(value) or True # OK
16 changes: 15 additions & 1 deletion src/rules/flake8_simplify/rules/ast_bool_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use itertools::Either::{Left, Right};
use rustc_hash::FxHashMap;
use rustpython_ast::{Boolop, Cmpop, Constant, Expr, ExprContext, ExprKind, Unaryop};

use crate::ast::helpers::{create_expr, unparse_expr};
use crate::ast::helpers::{contains_effect, create_expr, unparse_expr};
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::fix::Fix;
Expand Down Expand Up @@ -230,6 +230,10 @@ pub fn a_and_not_a(checker: &mut Checker, expr: &Expr) {
return;
}

if contains_effect(checker, expr) {
return;
}

for negate_expr in negated_expr {
for non_negate_expr in &non_negated_expr {
if let Some(id) = is_same_expr(negate_expr, non_negate_expr) {
Expand Down Expand Up @@ -278,6 +282,10 @@ pub fn a_or_not_a(checker: &mut Checker, expr: &Expr) {
return;
}

if contains_effect(checker, expr) {
return;
}

for negate_expr in negated_expr {
for non_negate_expr in &non_negated_expr {
if let Some(id) = is_same_expr(negate_expr, non_negate_expr) {
Expand All @@ -303,6 +311,9 @@ pub fn or_true(checker: &mut Checker, expr: &Expr) {
let ExprKind::BoolOp { op: Boolop::Or, values, } = &expr.node else {
return;
};
if contains_effect(checker, expr) {
return;
}
for value in values {
if let ExprKind::Constant {
value: Constant::Bool(true),
Expand All @@ -327,6 +338,9 @@ pub fn and_false(checker: &mut Checker, expr: &Expr) {
let ExprKind::BoolOp { op: Boolop::And, values, } = &expr.node else {
return;
};
if contains_effect(checker, expr) {
return;
}
for value in values {
if let ExprKind::Constant {
value: Constant::Bool(false),
Expand Down