Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

FMorschel
Copy link
Contributor

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

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] page, which explains my responsibilities.
  • I read and followed the [relevant style guides] and ran [the auto-formatter].
  • I signed the [CLA].
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I [linked to at least one issue that this PR fixes] in the description above.
  • I updated 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.
  • I updated 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.
  • I updated/added any relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or I have commented below to indicate which [test exemption] this PR falls under1.
  • All existing and new tests are passing.

Footnotes

  1. 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

Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a 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) {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

https://github.com/dart-lang/sdk/blob/da5ae4786cc41812eee6f83ed1e9c5c05c15f387/pkg/_fe_analyzer_shared/lib/src/scanner/token.dart#L21-L22

Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants