Skip to content

Commit

Permalink
[analyzer] Move 2 more Hints to be Warnings, DEAD_CODE*
Browse files Browse the repository at this point in the history
Bug: #50796
Change-Id: Ie7a6cb94cefaf4f551ed766e637bac3606c0f5ed
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279463
Commit-Queue: Samuel Rawlins <[email protected]>
Reviewed-by: Konstantin Shcheglov <[email protected]>
  • Loading branch information
srawlins authored and Commit Queue committed Mar 21, 2023
1 parent c7c2be0 commit 6333e31
Show file tree
Hide file tree
Showing 36 changed files with 450 additions and 454 deletions.
2 changes: 1 addition & 1 deletion pkg/analysis_server/lib/src/lsp/mapping.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import 'package:collection/collection.dart';
const languageSourceName = 'dart';

final diagnosticTagsForErrorCode = <String, List<lsp.DiagnosticTag>>{
_errorCode(HintCode.DEAD_CODE): [lsp.DiagnosticTag.Unnecessary],
_errorCode(WarningCode.DEAD_CODE): [lsp.DiagnosticTag.Unnecessary],
_errorCode(HintCode.DEPRECATED_MEMBER_USE): [lsp.DiagnosticTag.Deprecated],
_errorCode(HintCode.DEPRECATED_MEMBER_USE_FROM_SAME_PACKAGE): [
lsp.DiagnosticTag.Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1392,12 +1392,6 @@ FfiCode.SUBTYPE_OF_STRUCT_CLASS_IN_WITH:
status: hasFix
HintCode.CAN_BE_NULL_AFTER_NULL_AWARE:
status: hasFix
HintCode.DEAD_CODE:
status: hasFix
HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH:
status: hasFix
HintCode.DEAD_CODE_ON_CATCH_SUBTYPE:
status: hasFix
HintCode.DEPRECATED_COLON_FOR_DEFAULT_VALUE:
status: hasFix
HintCode.DEPRECATED_EXPORT_USE:
Expand Down Expand Up @@ -2555,6 +2549,12 @@ WarningCode.CONSTANT_PATTERN_NEVER_MATCHES_VALUE_TYPE:
status: needsEvaluation
WarningCode.BODY_MIGHT_COMPLETE_NORMALLY_NULLABLE:
status: hasFix
WarningCode.DEAD_CODE:
status: hasFix
WarningCode.DEAD_CODE_CATCH_FOLLOWING_CATCH:
status: hasFix
WarningCode.DEAD_CODE_ON_CATCH_SUBTYPE:
status: hasFix
WarningCode.DEPRECATED_EXTENDS_FUNCTION:
status: needsFix
notes: |-
Expand Down
26 changes: 13 additions & 13 deletions pkg/analysis_server/lib/src/services/correction/fix_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1361,19 +1361,6 @@ class FixProcessor extends BaseProcessor {
HintCode.CAN_BE_NULL_AFTER_NULL_AWARE: [
ReplaceWithNullAware.inChain,
],
HintCode.DEAD_CODE: [
RemoveDeadCode.new,
],
HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH: [
// TODO(brianwilkerson) Add a fix to move the unreachable catch clause to
// a place where it can be reached (when possible).
RemoveDeadCode.new,
],
HintCode.DEAD_CODE_ON_CATCH_SUBTYPE: [
// TODO(brianwilkerson) Add a fix to move the unreachable catch clause to
// a place where it can be reached (when possible).
RemoveDeadCode.new,
],
HintCode.DEPRECATED_COLON_FOR_DEFAULT_VALUE: [
ReplaceColonWithEquals.new,
],
Expand Down Expand Up @@ -1479,6 +1466,19 @@ class FixProcessor extends BaseProcessor {
WarningCode.BODY_MIGHT_COMPLETE_NORMALLY_NULLABLE: [
AddReturnNull.new,
],
WarningCode.DEAD_CODE: [
RemoveDeadCode.new,
],
WarningCode.DEAD_CODE_CATCH_FOLLOWING_CATCH: [
// TODO(brianwilkerson) Add a fix to move the unreachable catch clause to
// a place where it can be reached (when possible).
RemoveDeadCode.new,
],
WarningCode.DEAD_CODE_ON_CATCH_SUBTYPE: [
// TODO(brianwilkerson) Add a fix to move the unreachable catch clause to
// a place where it can be reached (when possible).
RemoveDeadCode.new,
],
WarningCode.DEPRECATED_IMPLEMENTS_FUNCTION: [
RemoveNameFromDeclarationClause.new,
],
Expand Down
2 changes: 1 addition & 1 deletion pkg/analysis_server/test/abstract_single_unit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class AbstractSingleUnitTest extends AbstractContextTest {
testUnit = result.unit;
if (verifyNoTestUnitErrors) {
expect(result.errors.where((AnalysisError error) {
return error.errorCode != HintCode.DEAD_CODE &&
return error.errorCode != WarningCode.DEAD_CODE &&
error.errorCode != WarningCode.UNUSED_CATCH_CLAUSE &&
error.errorCode != WarningCode.UNUSED_CATCH_STACK &&
error.errorCode != HintCode.UNUSED_ELEMENT &&
Expand Down
44 changes: 0 additions & 44 deletions pkg/analyzer/lib/src/dart/error/hint_codes.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,6 @@ class HintCode extends AnalyzerErrorCode {
correctionMessage: "Replace the '.' with a '?.' in the invocation.",
);

/// Dead code is code that is never reached, this can happen for instance if a
/// statement follows a return statement.
///
/// No parameters.
static const HintCode DEAD_CODE = HintCode(
'DEAD_CODE',
"Dead code.",
correctionMessage:
"Try removing the code, or fixing the code before it so that it can be "
"reached.",
hasPublishedDocs: true,
);

/// Dead code is code that is never reached. This case covers cases where the
/// user has catch clauses after `catch (e)` or `on Object catch (e)`.
///
/// No parameters.
static const HintCode DEAD_CODE_CATCH_FOLLOWING_CATCH = HintCode(
'DEAD_CODE_CATCH_FOLLOWING_CATCH',
"Dead code: Catch clauses after a 'catch (e)' or an 'on Object catch (e)' "
"are never reached.",
correctionMessage:
"Try reordering the catch clauses so that they can be reached, or "
"removing the unreachable catch clauses.",
hasPublishedDocs: true,
);

/// Dead code is code that is never reached. This case covers cases where the
/// user has an on-catch clause such as `on A catch (e)`, where a supertype of
/// `A` was already caught.
///
/// Parameters:
/// 0: name of the subtype
/// 1: name of the supertype
static const HintCode DEAD_CODE_ON_CATCH_SUBTYPE = HintCode(
'DEAD_CODE_ON_CATCH_SUBTYPE',
"Dead code: This on-catch block won't be executed because '{0}' is a "
"subtype of '{1}' and hence will have been caught already.",
correctionMessage:
"Try reordering the catch clauses so that this block can be reached, "
"or removing the unreachable catch clause.",
hasPublishedDocs: true,
);

/// No parameters.
static const HintCode DEPRECATED_COLON_FOR_DEFAULT_VALUE = HintCode(
'DEPRECATED_COLON_FOR_DEFAULT_VALUE',
Expand Down
45 changes: 43 additions & 2 deletions pkg/analyzer/lib/src/error/codes.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5828,8 +5828,49 @@ class WarningCode extends AnalyzerErrorCode {
"Try a constant of the same type as the matched value type.",
);

/// This is the new replacement for [HintCode.DEAD_CODE].
static const HintCode DEAD_CODE = HintCode.DEAD_CODE;
/// Dead code is code that is never reached, this can happen for instance if a
/// statement follows a return statement.
///
/// No parameters.
static const WarningCode DEAD_CODE = WarningCode(
'DEAD_CODE',
"Dead code.",
correctionMessage:
"Try removing the code, or fixing the code before it so that it can be "
"reached.",
hasPublishedDocs: true,
);

/// Dead code is code that is never reached. This case covers cases where the
/// user has catch clauses after `catch (e)` or `on Object catch (e)`.
///
/// No parameters.
static const WarningCode DEAD_CODE_CATCH_FOLLOWING_CATCH = WarningCode(
'DEAD_CODE_CATCH_FOLLOWING_CATCH',
"Dead code: Catch clauses after a 'catch (e)' or an 'on Object catch (e)' "
"are never reached.",
correctionMessage:
"Try reordering the catch clauses so that they can be reached, or "
"removing the unreachable catch clauses.",
hasPublishedDocs: true,
);

/// Dead code is code that is never reached. This case covers cases where the
/// user has an on-catch clause such as `on A catch (e)`, where a supertype of
/// `A` was already caught.
///
/// Parameters:
/// 0: name of the subtype
/// 1: name of the supertype
static const WarningCode DEAD_CODE_ON_CATCH_SUBTYPE = WarningCode(
'DEAD_CODE_ON_CATCH_SUBTYPE',
"Dead code: This on-catch block won't be executed because '{0}' is a "
"subtype of '{1}' and hence will have been caught already.",
correctionMessage:
"Try reordering the catch clauses so that this block can be reached, "
"or removing the unreachable catch clause.",
hasPublishedDocs: true,
);

/// No parameters.
static const WarningCode DEPRECATED_EXTENDS_FUNCTION = WarningCode(
Expand Down
36 changes: 19 additions & 17 deletions pkg/analyzer/lib/src/error/dead_code_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class LegacyDeadCodeVerifier extends RecursiveAstVisitor<void> {
var offset = node.operator.offset;
var length = node.rightOperand.end - offset;
_errorReporter.reportErrorForOffset(
HintCode.DEAD_CODE, offset, length);
WarningCode.DEAD_CODE, offset, length);
// Only visit the LHS:
lhsCondition.accept(this);
return;
Expand Down Expand Up @@ -217,13 +217,13 @@ class LegacyDeadCodeVerifier extends RecursiveAstVisitor<void> {
if (result.value?.toBoolValue() == true) {
// Report error on "else" block: true ? 1 : !2!
_errorReporter.reportErrorForNode(
HintCode.DEAD_CODE, node.elseExpression);
WarningCode.DEAD_CODE, node.elseExpression);
node.thenExpression.accept(this);
return;
} else {
// Report error on "if" block: false ? !1! : 2
_errorReporter.reportErrorForNode(
HintCode.DEAD_CODE, node.thenExpression);
WarningCode.DEAD_CODE, node.thenExpression);
node.elseExpression.accept(this);
return;
}
Expand All @@ -243,14 +243,15 @@ class LegacyDeadCodeVerifier extends RecursiveAstVisitor<void> {
// Report error on else block: if(true) {} else {!}
var elseElement = node.elseElement;
if (elseElement != null) {
_errorReporter.reportErrorForNode(HintCode.DEAD_CODE, elseElement);
_errorReporter.reportErrorForNode(
WarningCode.DEAD_CODE, elseElement);
node.thenElement.accept(this);
return;
}
} else {
// Report error on if block: if (false) {!} else {}
_errorReporter.reportErrorForNode(
HintCode.DEAD_CODE, node.thenElement);
WarningCode.DEAD_CODE, node.thenElement);
node.elseElement?.accept(this);
return;
}
Expand All @@ -271,14 +272,14 @@ class LegacyDeadCodeVerifier extends RecursiveAstVisitor<void> {
var elseStatement = node.elseStatement;
if (elseStatement != null) {
_errorReporter.reportErrorForNode(
HintCode.DEAD_CODE, elseStatement);
WarningCode.DEAD_CODE, elseStatement);
node.thenStatement.accept(this);
return;
}
} else {
// Report error on if block: if (false) {!} else {}
_errorReporter.reportErrorForNode(
HintCode.DEAD_CODE, node.thenStatement);
WarningCode.DEAD_CODE, node.thenStatement);
node.elseStatement?.accept(this);
return;
}
Expand Down Expand Up @@ -336,7 +337,7 @@ class LegacyDeadCodeVerifier extends RecursiveAstVisitor<void> {
if (result != null) {
if (result.value?.toBoolValue() == false) {
// Report error on while block: while (false) {!}
_errorReporter.reportErrorForNode(HintCode.DEAD_CODE, node.body);
_errorReporter.reportErrorForNode(WarningCode.DEAD_CODE, node.body);
return;
}
}
Expand Down Expand Up @@ -378,7 +379,8 @@ class LegacyDeadCodeVerifier extends RecursiveAstVisitor<void> {
}
int offset = nextStatement.offset;
int length = lastStatement.end - offset;
_errorReporter.reportErrorForOffset(HintCode.DEAD_CODE, offset, length);
_errorReporter.reportErrorForOffset(
WarningCode.DEAD_CODE, offset, length);
return;
}
}
Expand Down Expand Up @@ -489,7 +491,7 @@ class NullSafetyDeadCodeVerifier {
}

if (node is SwitchMember && node == firstDeadNode) {
_errorReporter.reportErrorForToken(HintCode.DEAD_CODE, node.keyword);
_errorReporter.reportErrorForToken(WarningCode.DEAD_CODE, node.keyword);
_firstDeadNode = null;
return;
}
Expand Down Expand Up @@ -537,9 +539,9 @@ class NullSafetyDeadCodeVerifier {
whileOffset = body.rightBracket.offset;
}
_errorReporter.reportErrorForOffset(
HintCode.DEAD_CODE, doOffset, doEnd - doOffset);
WarningCode.DEAD_CODE, doOffset, doEnd - doOffset);
_errorReporter.reportErrorForOffset(
HintCode.DEAD_CODE, whileOffset, whileEnd - whileOffset);
WarningCode.DEAD_CODE, whileOffset, whileEnd - whileOffset);
offset = parent.semicolon.next!.offset;
if (parent.hasBreakStatement) {
offset = node.end;
Expand All @@ -560,7 +562,7 @@ class NullSafetyDeadCodeVerifier {
var length = node.end - offset;
if (length > 0) {
_errorReporter.reportErrorForOffset(
HintCode.DEAD_CODE, offset, length);
WarningCode.DEAD_CODE, offset, length);
}
}

Expand Down Expand Up @@ -664,7 +666,7 @@ class NullSafetyDeadCodeVerifier {
var beginToken = updaters.beginToken;
var endToken = updaters.endToken;
if (beginToken != null && endToken != null) {
_errorReporter.reportErrorForOffset(HintCode.DEAD_CODE,
_errorReporter.reportErrorForOffset(WarningCode.DEAD_CODE,
beginToken.offset, endToken.end - beginToken.offset);
}
}
Expand Down Expand Up @@ -697,7 +699,7 @@ class NullSafetyDeadCodeVerifier {
node = parent!;
parent = node.parent;
}
_errorReporter.reportErrorForNode(HintCode.DEAD_CODE, node);
_errorReporter.reportErrorForNode(WarningCode.DEAD_CODE, node);
}
}
}
Expand Down Expand Up @@ -743,7 +745,7 @@ class _CatchClausesVerifier {
_errorReporter(
catchClauses[index + 1],
catchClauses.last,
HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH,
WarningCode.DEAD_CODE_CATCH_FOLLOWING_CATCH,
const [],
);
_done = true;
Expand All @@ -758,7 +760,7 @@ class _CatchClausesVerifier {
_errorReporter(
catchClause,
catchClauses.last,
HintCode.DEAD_CODE_ON_CATCH_SUBTYPE,
WarningCode.DEAD_CODE_ON_CATCH_SUBTYPE,
[currentType, type],
);
_done = true;
Expand Down
6 changes: 3 additions & 3 deletions pkg/analyzer/lib/src/error/error_code_values.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,6 @@ const List<ErrorCode> errorCodeValues = [
FfiCode.SUBTYPE_OF_STRUCT_CLASS_IN_IMPLEMENTS,
FfiCode.SUBTYPE_OF_STRUCT_CLASS_IN_WITH,
HintCode.CAN_BE_NULL_AFTER_NULL_AWARE,
HintCode.DEAD_CODE,
HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH,
HintCode.DEAD_CODE_ON_CATCH_SUBTYPE,
HintCode.DEPRECATED_COLON_FOR_DEFAULT_VALUE,
HintCode.DEPRECATED_EXPORT_USE,
HintCode.DEPRECATED_MEMBER_USE,
Expand Down Expand Up @@ -904,6 +901,9 @@ const List<ErrorCode> errorCodeValues = [
WarningCode.CAST_FROM_NULLABLE_ALWAYS_FAILS,
WarningCode.CAST_FROM_NULL_ALWAYS_FAILS,
WarningCode.CONSTANT_PATTERN_NEVER_MATCHES_VALUE_TYPE,
WarningCode.DEAD_CODE,
WarningCode.DEAD_CODE_CATCH_FOLLOWING_CATCH,
WarningCode.DEAD_CODE_ON_CATCH_SUBTYPE,
WarningCode.DEPRECATED_EXTENDS_FUNCTION,
WarningCode.DEPRECATED_IMPLEMENTS_FUNCTION,
WarningCode.DEPRECATED_MIXIN_FUNCTION,
Expand Down
Loading

0 comments on commit 6333e31

Please sign in to comment.