pkg:compiler - enable and fix null operator lints

Change-Id: I5aaef20fdd61fd6ac9dc69978111fb680c7f99e5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/211624
Auto-Submit: Kevin Moore <kevmoo@google.com>
Commit-Queue: Kevin Moore <kevmoo@google.com>
Reviewed-by: Joshua Litt <joshualitt@google.com>
This commit is contained in:
Kevin Moore 2021-09-24 23:07:19 +00:00 committed by commit-bot@chromium.org
parent 7786183e14
commit b85488cedd
22 changed files with 32 additions and 38 deletions

View file

@ -15,3 +15,5 @@ linter:
rules:
- annotate_overrides
- prefer_final_fields
- prefer_if_null_operators
- prefer_null_aware_operators

View file

@ -218,9 +218,8 @@ class _CodegenImpact extends WorldImpactBuilderImpl implements CodegenImpact {
@override
Iterable<Pair<DartType, DartType>> get typeVariableBoundsSubtypeChecks {
return _typeVariableBoundsSubtypeChecks != null
? _typeVariableBoundsSubtypeChecks
: const <Pair<DartType, DartType>>[];
return _typeVariableBoundsSubtypeChecks ??
const <Pair<DartType, DartType>>[];
}
void registerConstSymbol(String name) {
@ -230,7 +229,7 @@ class _CodegenImpact extends WorldImpactBuilderImpl implements CodegenImpact {
@override
Iterable<String> get constSymbols {
return _constSymbols != null ? _constSymbols : const <String>[];
return _constSymbols ?? const <String>[];
}
void registerSpecializedGetInterceptor(Set<ClassEntity> classes) {
@ -240,9 +239,7 @@ class _CodegenImpact extends WorldImpactBuilderImpl implements CodegenImpact {
@override
Iterable<Set<ClassEntity>> get specializedGetInterceptors {
return _specializedGetInterceptors != null
? _specializedGetInterceptors
: const <Set<ClassEntity>>[];
return _specializedGetInterceptors ?? const <Set<ClassEntity>>[];
}
void registerUseInterceptor() {

View file

@ -962,7 +962,7 @@ class CompilerDiagnosticReporter extends DiagnosticReporter {
} else if (node is HInstruction) {
element = node.sourceElement;
}
return element != null ? element : currentElement;
return element ?? currentElement;
}
@override

View file

@ -1109,9 +1109,7 @@ class InferrerEngine {
} else if (selector.isGetter) {
if (element.isFunction) {
// [functionType] is null if the inferrer did not run.
return types.functionType == null
? types.dynamicType
: types.functionType;
return types.functionType ?? types.dynamicType;
} else if (element.isField) {
return typeOfMember(element);
} else if (element.isGetter) {

View file

@ -45,7 +45,7 @@ class PowersetValue implements AbstractValue {
}
AbstractValue unwrapOrNull(PowersetValue powerset) {
return powerset == null ? null : powerset._abstractValue;
return powerset?._abstractValue;
}
PowersetValue wrapOrNull(AbstractValue abstractValue, int powersetBits) {

View file

@ -37,7 +37,7 @@ class WrappedAbstractValue implements AbstractValue {
}
AbstractValue unwrapOrNull(WrappedAbstractValue wrapped) {
return wrapped == null ? null : wrapped._abstractValue;
return wrapped?._abstractValue;
}
WrappedAbstractValue wrapOrNull(AbstractValue abstractValue) {

View file

@ -47,7 +47,7 @@ abstract class TypeInformation {
final MemberTypeInformation context;
/// The element this [TypeInformation] node belongs to.
MemberEntity get contextMember => context == null ? null : context.member;
MemberEntity get contextMember => context?.member;
Iterable<TypeInformation> get inputs => _inputs;

View file

@ -77,8 +77,7 @@ class PositionSourceInformation extends SourceInformation {
@override
SourceSpan get sourceSpan {
SourceLocation location =
startPosition != null ? startPosition : innerPosition;
SourceLocation location = startPosition ?? innerPosition;
Uri uri = location.sourceUri;
int offset = location.offset;
return SourceSpan(uri, offset, offset);

View file

@ -402,7 +402,7 @@ class ScopeModelBuilder extends ir.Visitor<EvaluationComplexity>
// If this typeParameter is part of a typedef then its parent is
// null because it has no context. Just pass in null for the
// context in that case.
typeParameter.parent != null ? typeParameter.parent.parent : null);
typeParameter.parent?.parent);
ir.TreeNode context = _executableContext;
if (_isInsideClosure && context is ir.Procedure && context.isFactory) {

View file

@ -777,7 +777,7 @@ class TypeCheckMapping implements TypeChecks {
@override
ClassChecks operator [](ClassEntity element) {
ClassChecks result = map[element];
return result != null ? result : const ClassChecks.empty();
return result ?? const ClassChecks.empty();
}
void operator []=(ClassEntity element, ClassChecks checks) {

View file

@ -406,7 +406,7 @@ class JsClosedWorld implements JClosedWorld {
}
}
Iterable<ClassEntity> uses = _liveMixinUses[cls];
return uses != null ? uses : const <ClassEntity>[];
return uses ?? const <ClassEntity>[];
}
@override

View file

@ -6426,15 +6426,13 @@ class TryCatchFinallyBuilder {
// The body has either the catch or the finally block as successor.
if (endTryBlock != null) {
assert(startCatchBlock != null || startFinallyBlock != null);
endTryBlock.addSuccessor(
startCatchBlock != null ? startCatchBlock : startFinallyBlock);
endTryBlock.addSuccessor(startCatchBlock ?? startFinallyBlock);
endTryBlock.addSuccessor(exitBlock);
}
// The catch block has either the finally or the exit block as
// successor.
endCatchBlock?.addSuccessor(
startFinallyBlock != null ? startFinallyBlock : exitBlock);
endCatchBlock?.addSuccessor(startFinallyBlock ?? exitBlock);
// The finally block has the exit block as successor.
endFinallyBlock?.addSuccessor(exitBlock);

View file

@ -565,7 +565,7 @@ class SsaInstructionSimplifier extends HBaseVisitor
@override
HInstruction visitInvokeUnary(HInvokeUnary node) {
HInstruction folded = foldUnary(node.operation(), node.operand);
return folded != null ? folded : node;
return folded ?? node;
}
HInstruction foldUnary(
@ -1161,7 +1161,7 @@ class SsaInstructionSimplifier extends HBaseVisitor
@override
HInstruction visitIdentity(HIdentity node) {
HInstruction newInstruction = handleIdentityCheck(node);
return newInstruction == null ? super.visitIdentity(node) : newInstruction;
return newInstruction ?? super.visitIdentity(node);
}
@override

View file

@ -101,7 +101,7 @@ class LiveEnvironment {
liveIntervals.putIfAbsent(instruction, () => LiveInterval());
int lastId = liveInstructions[instruction];
// If [lastId] is null, then this instruction is not being used.
interval.add(LiveRange(id, lastId == null ? id : lastId));
interval.add(LiveRange(id, lastId ?? id));
// The instruction is defined at [id].
interval.start = id;
liveInstructions.remove(instruction);

View file

@ -906,7 +906,7 @@ class ClassHierarchyNodeIterator implements Iterator<ClassEntity> {
@override
ClassEntity get current {
return currentNode != null ? currentNode.cls : null;
return currentNode?.cls;
}
@override

View file

@ -16,3 +16,5 @@ analyzer:
linter:
rules:
- annotate_overrides
- prefer_if_null_operators
- prefer_null_aware_operators

View file

@ -17,8 +17,8 @@ int checkEqualContentAndShowDiff(String text1, String text2,
if (filter != null && filter(i, lines1, lines2)) {
String line1 = 0 <= i && i < lines1.length ? lines1[i] : null;
String line2 = 0 <= i && i < lines2.length ? lines2[i] : null;
String text = line1 == null ? '<eof>' : line1;
String newText = line2 == null ? '<eof>' : line2;
String text = line1 ?? '<eof>';
String newText = line2 ?? '<eof>';
print('(skipped) - $i ${text}');
print('(skipped) + $i ${newText}');
} else {
@ -41,8 +41,8 @@ int checkEqualContentAndShowDiff(String text1, String text2,
print(' $j $line1');
}
} else {
String text = line1 == null ? '<eof>' : line1;
String newText = line2 == null ? '<eof>' : line2;
String text = line1 ?? '<eof>';
String newText = line2 ?? '<eof>';
if (text.length > 80 && newText.length > 80) {
flushPendingLines();

View file

@ -96,9 +96,7 @@ void testWithSideEffects(DartTypes dartTypes, String specString,
returns: returns,
creates: creates,
expectedSideEffects: expectedSideEffects,
expectError: sideEffectsExpectError == null
? expectError
: sideEffectsExpectError);
expectError: sideEffectsExpectError ?? expectError);
}
SideEffects emptySideEffects = new SideEffects.empty();

View file

@ -386,7 +386,7 @@ class Test {
this.warnings: const <MessageKind>[]})
: _source = null;
String get source => _source != null ? _source : _sources['main.dart'];
String get source => _source ?? _sources['main.dart'];
Map<String, String> get sources =>
_source != null ? {'main.dart': _source} : _sources;

View file

@ -96,7 +96,7 @@ testIterators() async {
void checkState(ClassEntity root,
{ClassEntity currentNode, List<ClassEntity> stack}) {
ClassEntity classOf(ClassHierarchyNode node) {
return node != null ? node.cls : null;
return node?.cls;
}
List<ClassEntity> classesOf(Iterable<ClassHierarchyNode> list) {

View file

@ -37,7 +37,7 @@ class DiffColumn {
}
@override
String toString() => '$type${index != null ? index : ''}';
String toString() => '$type${index ?? ''}';
}
/// A block of code in an output column.

View file

@ -282,7 +282,7 @@ abstract class AbstractEntity extends OutputEntity {
'from': from,
'to': to,
'children': children.map((child) => child.toJson(strategy)).toList(),
'codeSource': codeSource != null ? codeSource.toJson() : null,
'codeSource': codeSource?.toJson(),
};
}