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

add use_if_null_to_convert_null_to_bool #2480

Merged
merged 3 commits into from Mar 1, 2021

Conversation

a14n
Copy link
Contributor

@a14n a14n commented Feb 25, 2021

Description

Use if-null operator to convert null to bool.

BAD:

if (nullableBool == true) {
}

GOOD:

if (nullableBool ?? false) {
}

Fixes #2460

@google-cla google-cla bot added the cla: yes label Feb 25, 2021
@coveralls
Copy link

coveralls commented Feb 25, 2021

Coverage Status

Coverage increased (+0.008%) to 96.617% when pulling 5424869 on a14n:use_if_null_to_convert_null_to_bool into 087a1c6 on dart-lang:master.

Copy link
Member

@bwilkerson bwilkerson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to catch cases where the operands are reversed (true == e)?
Do we want to catch uses of != (e != false)?

@a14n
Copy link
Contributor Author

a14n commented Feb 25, 2021

Do we want to catch cases where the operands are reversed (true == e)?

I don't want to bother yoda conditions lovers :) (this could be another lint BTW - avoid_yoda_conditions)

Do we want to catch uses of != (e != false)?

I will add some code to catch that.

Copy link
Member

@pq pq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming to use_if_null_to_convert_nulls_to_bools?

I feel like we've mostly pluralized. For example:

use_key_in_widget_constructors
use_late_for_private_fields_and_variables
use_setters_to_change_properties

etc.


import '../analyzer.dart';

const _desc = r'Use if-null operator to convert null to bool.';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=>

'Use if-null operators to convert nulls to bools.';

(We standardized on plurals -- mostly 😬 )


const _details = r'''

Use if-null operator to convert null to bool.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=>

Use if-null operators to convert nulls to bools.

@pq
Copy link
Member

pq commented Feb 25, 2021

PS: this is super awesome!

Copy link
Member

@pq pq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@pq
Copy link
Member

pq commented Mar 1, 2021

Thanks!

@pq pq merged commit 8a9c450 into dart-lang:master Mar 1, 2021
mockturtl added a commit to mockturtl/tidy that referenced this pull request Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

new lint: if_null_boolean_conversions (use ?? to convert null to a boolean value.)
4 participants