-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Removes unnecessary boolean operations #9361
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every change here changed the semantics of the code.
@@ -78,7 +78,7 @@ Future<bool> launch( | |||
final bool isWebURL = | |||
url != null && (url.scheme == 'http' || url.scheme == 'https'); | |||
|
|||
if ((forceSafariVC ?? false || forceWebView) && !isWebURL) { | |||
if ((forceSafariVC ?? forceWebView) && !isWebURL) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have changed the behavior of this code; forceSafariVC = false
and forceWebView = true
now evaluates, incorrectly, to false
.
I don't understand how this tripped the lint in the first place. What is wrong with using ?? <default value>
with a nullable boolean? Is there a bug in the linter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not as I can see. The precedence in the operators (as I tipped in the PR description) actually does this.
The first thing that will occur is false || forceWebView
and only then forceSafariVC ?? expr
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, that's very unintuitive. That means that the code was unfortunately incorrect and we just hadn't noticed. Linter improvement to the rescue!
In that case the fix for the lint here is to add the parens to make the code work as intended, where forceSafariVC ?? false
is one logical unit, and to add a test like this one for the broken case to make sure we don't regress it later.
I'm fixing dart-lang/sdk#60614, which adds some other cases to
no_literal_bool_comparisons
, and these were triggered here.I'm unsure of the intended behaviour of the
packages\url_launcher\url_launcher\lib\src\legacy_api.dart
part, but I can fix it if the result wasn't intended this way (the operators' priority may be less known here).About the other cases with
|| true
, I think these might be a pattern from the team, so maybe adding a file ignore could also fix this. Please say so that I can make this the right way for you.Pre-Review Checklist
[shared_preferences]
pubspec.yaml
with an appropriate new version according to the [pub versioning philosophy], or I have commented below to indicate which [version change exemption] this PR falls under1.CHANGELOG.md
to add a description of the change, [following repository CHANGELOG style], or I have commented below to indicate which [CHANGELOG exemption] this PR falls under1.///
).Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2 ↩3