Skip to content

false positive invariant_booleans on nested if #58014

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

Closed
itsnuba opened this issue Sep 12, 2019 · 2 comments
Closed

false positive invariant_booleans on nested if #58014

itsnuba opened this issue Sep 12, 2019 · 2 comments
Labels
devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. linter-false-positive P4 type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@itsnuba
Copy link

itsnuba commented Sep 12, 2019

invariant_booleans fired when im using if statement with or operation then using the same statement with and condition

To Reproduce

const a = true;
const b = false;
var text = "";
if (a || b) {
  text += "success! also : ";
  if (a && b) {
    text += "both value is true";
  } else if (a) {
    text += "only a is true";
  } else {
    text += "only b is true";
  }
} else {
  text += "error!";
}
print(text);

a && b is detected as invariant_booleans

Expected behavior
invariant_booleans should not be fired, because the condition is not redundant with previous condition

Additional context
when the inner statement is only if, without else / else if, invariant_booleans isnt fired

const a = true;
const b = false;
var text = "";
if (a || b) {
  text += "success! also : ";
  if (a && b) {
    text += "both value is true";
  }
} else {
  text += "error!";
}
print(text);
@pq pq added linter-false-positive type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Sep 12, 2019
@jamesderlin
Copy link
Contributor

invariant_booleans would be appropriate for the above code as written. If anything, it should be complaining about the if (a || b) check too since a and b are const, and therefore all of the conditions are known at compilation time. Currently (using Dart SDK 2.16.2), I don't get invariant_booleans warnings but do get dead_code warnings, which is even better.

I do get an invariant_booleans warning on the if (a && b) check with:

bool getTrue() => true;
bool getFalse() => false;

void main() {
  var a = getTrue();
  var b = getFalse();
  var text = "";
  if (a || b) {
    text += "success! also : ";
    if (a && b) {
      text += "both value is true";
    } else if (a) {
      text += "only a is true";
    } else {
      text += "only b is true";
    }
  } else {
    text += "error!";
  }
  print(text);
}

@srawlins srawlins added the P4 label Sep 25, 2022
@srawlins
Copy link
Member

We've deprecated invariant_booleans and will not be fixing correctness bugs going forward.

@devoncarew devoncarew added devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. labels Nov 18, 2024
@devoncarew devoncarew transferred this issue from dart-archive/linter Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. linter-false-positive P4 type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants