Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

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 dart-lang/sdk#58324

@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
Contributor

@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
Contributor

@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
Contributor

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
Contributor

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
Contributor

pq commented Feb 25, 2021

PS: this is super awesome!

Copy link
Contributor

@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
Contributor

pq commented Mar 1, 2021

Thanks!

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

Labels

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