mirror of
https://github.com/dart-lang/sdk
synced 2024-09-15 22:41:41 +00:00
[analyzer] change range of dead code of the RHS of binary operator
Bug: #43511 Change-Id: I1b1562c0262101900dabea3544bfc28293d5c7f6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/260109 Commit-Queue: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
parent
24b8399086
commit
ecb992586a
|
@ -19,6 +19,23 @@ class RemoveDeadCode extends CorrectionProducer {
|
||||||
// Not predictably the correct action.
|
// Not predictably the correct action.
|
||||||
bool get canBeAppliedToFile => false;
|
bool get canBeAppliedToFile => false;
|
||||||
|
|
||||||
|
@override
|
||||||
|
AstNode? get coveredNode {
|
||||||
|
var node = super.coveredNode;
|
||||||
|
if (node is BinaryExpression) {
|
||||||
|
var problemMessage = diagnostic?.problemMessage;
|
||||||
|
if (problemMessage != null) {
|
||||||
|
var operatorOffset = node.operator.offset;
|
||||||
|
var rightOperand = node.rightOperand;
|
||||||
|
if (problemMessage.offset == operatorOffset &&
|
||||||
|
problemMessage.length == rightOperand.end - operatorOffset) {
|
||||||
|
return rightOperand;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
FixKind get fixKind => DartFixKind.REMOVE_DEAD_CODE;
|
FixKind get fixKind => DartFixKind.REMOVE_DEAD_CODE;
|
||||||
|
|
||||||
|
|
|
@ -160,17 +160,13 @@ class LegacyDeadCodeVerifier extends RecursiveAstVisitor<void> {
|
||||||
var lhsResult = _getConstantBooleanValue(lhsCondition);
|
var lhsResult = _getConstantBooleanValue(lhsCondition);
|
||||||
if (lhsResult != null) {
|
if (lhsResult != null) {
|
||||||
var value = lhsResult.value?.toBoolValue();
|
var value = lhsResult.value?.toBoolValue();
|
||||||
if (value == true && isBarBar) {
|
// Report error on "else" block: true || !e!
|
||||||
// Report error on "else" block: true || !e!
|
// or on "if" block: false && !e!
|
||||||
_errorReporter.reportErrorForNode(
|
if (value == true && isBarBar || value == false && isAmpAmp) {
|
||||||
HintCode.DEAD_CODE, node.rightOperand);
|
var offset = node.operator.offset;
|
||||||
// Only visit the LHS:
|
var length = node.rightOperand.end - offset;
|
||||||
lhsCondition.accept(this);
|
_errorReporter.reportErrorForOffset(
|
||||||
return;
|
HintCode.DEAD_CODE, offset, length);
|
||||||
} else if (value == false && isAmpAmp) {
|
|
||||||
// Report error on "if" block: false && !e!
|
|
||||||
_errorReporter.reportErrorForNode(
|
|
||||||
HintCode.DEAD_CODE, node.rightOperand);
|
|
||||||
// Only visit the LHS:
|
// Only visit the LHS:
|
||||||
lhsCondition.accept(this);
|
lhsCondition.accept(this);
|
||||||
return;
|
return;
|
||||||
|
@ -496,6 +492,7 @@ class NullSafetyDeadCodeVerifier {
|
||||||
// because this causes nuisance warnings for redundant `!= null`
|
// because this causes nuisance warnings for redundant `!= null`
|
||||||
// asserts.
|
// asserts.
|
||||||
} else {
|
} else {
|
||||||
|
var offset = firstDeadNode.offset;
|
||||||
// We know that [node] is the first dead node, or contains it.
|
// We know that [node] is the first dead node, or contains it.
|
||||||
// So, technically the code code interval ends at the end of [node].
|
// So, technically the code code interval ends at the end of [node].
|
||||||
// But we trim it to the last statement for presentation purposes.
|
// But we trim it to the last statement for presentation purposes.
|
||||||
|
@ -518,9 +515,10 @@ class NullSafetyDeadCodeVerifier {
|
||||||
if (node is SwitchMember && node.statements.isNotEmpty) {
|
if (node is SwitchMember && node.statements.isNotEmpty) {
|
||||||
node = node.statements.last;
|
node = node.statements.last;
|
||||||
}
|
}
|
||||||
|
} else if (parent is BinaryExpression && node == parent.rightOperand) {
|
||||||
|
offset = parent.operator.offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
var offset = firstDeadNode.offset;
|
|
||||||
var length = node.end - offset;
|
var length = node.end - offset;
|
||||||
_errorReporter.reportErrorForOffset(HintCode.DEAD_CODE, offset, length);
|
_errorReporter.reportErrorForOffset(HintCode.DEAD_CODE, offset, length);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2582,7 +2582,7 @@ class A {
|
||||||
const A.b2(bool p) : v = true || p;
|
const A.b2(bool p) : v = true || p;
|
||||||
}
|
}
|
||||||
''', [
|
''', [
|
||||||
error(HintCode.DEAD_CODE, 170, 1),
|
error(HintCode.DEAD_CODE, 167, 4),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ const c = true && '';
|
||||||
await assertErrorsInCode('''
|
await assertErrorsInCode('''
|
||||||
const c = (true || 0);
|
const c = (true || 0);
|
||||||
''', [
|
''', [
|
||||||
error(HintCode.DEAD_CODE, 19, 1),
|
error(HintCode.DEAD_CODE, 16, 4),
|
||||||
error(CompileTimeErrorCode.NON_BOOL_OPERAND, 19, 1),
|
error(CompileTimeErrorCode.NON_BOOL_OPERAND, 19, 1),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -494,7 +494,7 @@ f() {
|
||||||
bool b = false && false;
|
bool b = false && false;
|
||||||
print(b);
|
print(b);
|
||||||
}''', [
|
}''', [
|
||||||
error(HintCode.DEAD_CODE, 26, 5),
|
error(HintCode.DEAD_CODE, 23, 8),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -513,7 +513,7 @@ f() {
|
||||||
bool b = false && (false && false);
|
bool b = false && (false && false);
|
||||||
print(b);
|
print(b);
|
||||||
}''', [
|
}''', [
|
||||||
error(HintCode.DEAD_CODE, 26, 16),
|
error(HintCode.DEAD_CODE, 23, 19),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -523,7 +523,7 @@ f() {
|
||||||
bool b = true || true;
|
bool b = true || true;
|
||||||
print(b);
|
print(b);
|
||||||
}''', [
|
}''', [
|
||||||
error(HintCode.DEAD_CODE, 25, 4),
|
error(HintCode.DEAD_CODE, 22, 7),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -544,7 +544,7 @@ f() {
|
||||||
bool b = true || (false && false);
|
bool b = true || (false && false);
|
||||||
print(b);
|
print(b);
|
||||||
}''', [
|
}''', [
|
||||||
error(HintCode.DEAD_CODE, 25, 16),
|
error(HintCode.DEAD_CODE, 22, 19),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1514,7 +1514,7 @@ m() {
|
||||||
if(x || false) {}
|
if(x || false) {}
|
||||||
}
|
}
|
||||||
''', [
|
''', [
|
||||||
error(HintCode.DEAD_CODE, 33, 5),
|
error(HintCode.DEAD_CODE, 30, 8),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue