From a4ca3ded5f2778e01cad4f1450f75adb81aacc9e Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Thu, 19 Jan 2023 20:04:17 +0000 Subject: [PATCH] [analyzer] Move two more Hints to be Warnings, EQUAL_* Bug: https://github.com/dart-lang/sdk/issues/50796 Change-Id: Id07ac5940eafed58b1473804248381dd933cb311 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279229 Commit-Queue: Samuel Rawlins Reviewed-by: Konstantin Shcheglov --- .../services/correction/error_fix_status.yaml | 18 +- .../lib/src/dart/error/hint_codes.g.dart | 16 -- .../src/error/best_practices_verifier.dart | 4 +- pkg/analyzer/lib/src/error/codes.g.dart | 16 ++ .../lib/src/error/error_code_values.g.dart | 4 +- pkg/analyzer/messages.yaml | 182 +++++++++--------- .../equal_elements_in_const_set_test.dart | 2 +- .../equal_elements_in_set_test.dart | 8 +- .../equal_keys_in_const_map_test.dart | 2 +- .../diagnostics/equal_keys_in_map_test.dart | 8 +- 10 files changed, 130 insertions(+), 130 deletions(-) diff --git a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml index 85b03ac1b940..8fba8d1183a9 100644 --- a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml +++ b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml @@ -1370,14 +1370,6 @@ HintCode.DEPRECATED_MEMBER_USE_WITH_MESSAGE: status: hasFix HintCode.DIVISION_OPTIMIZATION: status: hasFix -HintCode.EQUAL_ELEMENTS_IN_SET: - status: noFix - notes: |- - Fix depends on user's intent, which can't be known. -HintCode.EQUAL_KEYS_IN_MAP: - status: noFix - notes: |- - Fix depends on user's intent, which can't be known. HintCode.FILE_IMPORT_INSIDE_LIB_REFERENCES_FILE_OUTSIDE: status: noFix notes: |- @@ -2761,6 +2753,14 @@ WarningCode.DUPLICATE_IMPORT: status: hasFix WarningCode.DUPLICATE_SHOWN_NAME: status: hasFix +WarningCode.EQUAL_ELEMENTS_IN_SET: + status: noFix + notes: |- + Fix depends on user's intent, which can't be known. +WarningCode.EQUAL_KEYS_IN_MAP: + status: noFix + notes: |- + Fix depends on user's intent, which can't be known. WarningCode.SDK_VERSION_ASYNC_EXPORTED_FROM_CORE: status: hasFix WarningCode.SDK_VERSION_AS_EXPRESSION_IN_CONST_CONTEXT: @@ -2787,4 +2787,4 @@ WarningCode.SDK_VERSION_UI_AS_CODE: status: hasFix WarningCode.SDK_VERSION_UI_AS_CODE_IN_CONST_CONTEXT: status: noFix - notes: Deprecated \ No newline at end of file + notes: Deprecated diff --git a/pkg/analyzer/lib/src/dart/error/hint_codes.g.dart b/pkg/analyzer/lib/src/dart/error/hint_codes.g.dart index 304b41646db4..0bd3caf039d1 100644 --- a/pkg/analyzer/lib/src/dart/error/hint_codes.g.dart +++ b/pkg/analyzer/lib/src/dart/error/hint_codes.g.dart @@ -173,22 +173,6 @@ class HintCode extends AnalyzerErrorCode { hasPublishedDocs: true, ); - /// No parameters. - static const HintCode EQUAL_ELEMENTS_IN_SET = HintCode( - 'EQUAL_ELEMENTS_IN_SET', - "Two elements in a set literal shouldn't be equal.", - correctionMessage: "Change or remove the duplicate element.", - hasPublishedDocs: true, - ); - - /// No parameters. - static const HintCode EQUAL_KEYS_IN_MAP = HintCode( - 'EQUAL_KEYS_IN_MAP', - "Two keys in a map literal shouldn't be equal.", - correctionMessage: "Change or remove the duplicate key.", - hasPublishedDocs: true, - ); - /// It is a bad practice for a source file in a package "lib" directory /// hierarchy to traverse outside that directory hierarchy. For example, a /// source file in the "lib" directory should not contain a directive such as diff --git a/pkg/analyzer/lib/src/error/best_practices_verifier.dart b/pkg/analyzer/lib/src/error/best_practices_verifier.dart index b9c41650ef5b..7e468b3dbeb7 100644 --- a/pkg/analyzer/lib/src/error/best_practices_verifier.dart +++ b/pkg/analyzer/lib/src/error/best_practices_verifier.dart @@ -990,8 +990,8 @@ class BestPracticesVerifier extends RecursiveAstVisitor { var value = constEvaluation.value; if (value != null && !alreadySeen.add(value)) { var errorCode = node.isSet - ? HintCode.EQUAL_ELEMENTS_IN_SET - : HintCode.EQUAL_KEYS_IN_MAP; + ? WarningCode.EQUAL_ELEMENTS_IN_SET + : WarningCode.EQUAL_KEYS_IN_MAP; _errorReporter.reportErrorForNode(errorCode, expression); } } diff --git a/pkg/analyzer/lib/src/error/codes.g.dart b/pkg/analyzer/lib/src/error/codes.g.dart index 229bd940acbe..fff53af73899 100644 --- a/pkg/analyzer/lib/src/error/codes.g.dart +++ b/pkg/analyzer/lib/src/error/codes.g.dart @@ -5693,6 +5693,22 @@ class WarningCode extends AnalyzerErrorCode { hasPublishedDocs: true, ); + /// No parameters. + static const WarningCode EQUAL_ELEMENTS_IN_SET = WarningCode( + 'EQUAL_ELEMENTS_IN_SET', + "Two elements in a set literal shouldn't be equal.", + correctionMessage: "Change or remove the duplicate element.", + hasPublishedDocs: true, + ); + + /// No parameters. + static const WarningCode EQUAL_KEYS_IN_MAP = WarningCode( + 'EQUAL_KEYS_IN_MAP', + "Two keys in a map literal shouldn't be equal.", + correctionMessage: "Change or remove the duplicate key.", + hasPublishedDocs: true, + ); + /// Parameters: /// 0: the name of the class static const WarningCode SDK_VERSION_ASYNC_EXPORTED_FROM_CORE = WarningCode( diff --git a/pkg/analyzer/lib/src/error/error_code_values.g.dart b/pkg/analyzer/lib/src/error/error_code_values.g.dart index 3fd220673da5..1368609b5494 100644 --- a/pkg/analyzer/lib/src/error/error_code_values.g.dart +++ b/pkg/analyzer/lib/src/error/error_code_values.g.dart @@ -579,8 +579,6 @@ const List errorCodeValues = [ HintCode.DEPRECATED_MEMBER_USE_FROM_SAME_PACKAGE_WITH_MESSAGE, HintCode.DEPRECATED_MEMBER_USE_WITH_MESSAGE, HintCode.DIVISION_OPTIMIZATION, - HintCode.EQUAL_ELEMENTS_IN_SET, - HintCode.EQUAL_KEYS_IN_MAP, HintCode.FILE_IMPORT_INSIDE_LIB_REFERENCES_FILE_OUTSIDE, HintCode.FILE_IMPORT_OUTSIDE_LIB_REFERENCES_FILE_INSIDE, HintCode.IMPORT_DEFERRED_LIBRARY_WITH_LOAD_FUNCTION, @@ -972,6 +970,8 @@ const List errorCodeValues = [ WarningCode.DUPLICATE_IGNORE, WarningCode.DUPLICATE_IMPORT, WarningCode.DUPLICATE_SHOWN_NAME, + WarningCode.EQUAL_ELEMENTS_IN_SET, + WarningCode.EQUAL_KEYS_IN_MAP, WarningCode.SDK_VERSION_ASYNC_EXPORTED_FROM_CORE, WarningCode.SDK_VERSION_AS_EXPRESSION_IN_CONST_CONTEXT, WarningCode.SDK_VERSION_BOOL_OPERATOR_IN_CONST_CONTEXT, diff --git a/pkg/analyzer/messages.yaml b/pkg/analyzer/messages.yaml index 4f537281485e..a7bf97521ee0 100644 --- a/pkg/analyzer/messages.yaml +++ b/pkg/analyzer/messages.yaml @@ -18116,97 +18116,6 @@ HintCode: ```dart int divide(num x, num y) => x ~/ y; ``` - EQUAL_ELEMENTS_IN_SET: - problemMessage: "Two elements in a set literal shouldn't be equal." - correctionMessage: Change or remove the duplicate element. - hasPublishedDocs: true - comment: No parameters. - documentation: |- - #### Description - - The analyzer produces this diagnostic when an element in a non-constant set - is the same as a previous element in the same set. If two elements are the - same, then the second value is ignored, which makes having both elements - pointless and likely signals a bug. - - #### Example - - The following code produces this diagnostic because the element `1` appears - twice: - - ```dart - const a = 1; - const b = 1; - var s = {a, [!b!]}; - ``` - - #### Common fixes - - If both elements should be included in the set, then change one of the - elements: - - ```dart - const a = 1; - const b = 2; - var s = {a, b}; - ``` - - If only one of the elements is needed, then remove the one that isn't - needed: - - ```dart - const a = 1; - var s = {a}; - ``` - - Note that literal sets preserve the order of their elements, so the choice - of which element to remove might affect the order in which elements are - returned by an iterator. - EQUAL_KEYS_IN_MAP: - problemMessage: "Two keys in a map literal shouldn't be equal." - correctionMessage: Change or remove the duplicate key. - hasPublishedDocs: true - comment: No parameters. - documentation: |- - #### Description - - The analyzer produces this diagnostic when a key in a non-constant map is - the same as a previous key in the same map. If two keys are the same, then - the second value overwrites the first value, which makes having both pairs - pointless and likely signals a bug. - - #### Example - - The following code produces this diagnostic because the keys `a` and `b` - have the same value: - - ```dart - const a = 1; - const b = 1; - var m = {a: 'a', [!b!]: 'b'}; - ``` - - #### Common fixes - - If both entries should be included in the map, then change one of the keys: - - ```dart - const a = 1; - const b = 2; - var m = {a: 'a', b: 'b'}; - ``` - - If only one of the entries is needed, then remove the one that isn't - needed: - - ```dart - const a = 1; - var m = {a: 'a'}; - ``` - - Note that literal maps preserve the order of their entries, so the choice - of which entry to remove might affect the order in which the keys and - values are returned by an iterator. FILE_IMPORT_INSIDE_LIB_REFERENCES_FILE_OUTSIDE: problemMessage: "A file in the 'lib' directory shouldn't import a file outside the 'lib' directory." correctionMessage: "Try removing the import, or moving the imported file inside the 'lib' directory." @@ -22838,6 +22747,97 @@ WarningCode: var x = min(2, min(0, 1)); ``` + EQUAL_ELEMENTS_IN_SET: + problemMessage: "Two elements in a set literal shouldn't be equal." + correctionMessage: Change or remove the duplicate element. + hasPublishedDocs: true + comment: No parameters. + documentation: |- + #### Description + + The analyzer produces this diagnostic when an element in a non-constant set + is the same as a previous element in the same set. If two elements are the + same, then the second value is ignored, which makes having both elements + pointless and likely signals a bug. + + #### Example + + The following code produces this diagnostic because the element `1` appears + twice: + + ```dart + const a = 1; + const b = 1; + var s = {a, [!b!]}; + ``` + + #### Common fixes + + If both elements should be included in the set, then change one of the + elements: + + ```dart + const a = 1; + const b = 2; + var s = {a, b}; + ``` + + If only one of the elements is needed, then remove the one that isn't + needed: + + ```dart + const a = 1; + var s = {a}; + ``` + + Note that literal sets preserve the order of their elements, so the choice + of which element to remove might affect the order in which elements are + returned by an iterator. + EQUAL_KEYS_IN_MAP: + problemMessage: "Two keys in a map literal shouldn't be equal." + correctionMessage: Change or remove the duplicate key. + hasPublishedDocs: true + comment: No parameters. + documentation: |- + #### Description + + The analyzer produces this diagnostic when a key in a non-constant map is + the same as a previous key in the same map. If two keys are the same, then + the second value overwrites the first value, which makes having both pairs + pointless and likely signals a bug. + + #### Example + + The following code produces this diagnostic because the keys `a` and `b` + have the same value: + + ```dart + const a = 1; + const b = 1; + var m = {a: 'a', [!b!]: 'b'}; + ``` + + #### Common fixes + + If both entries should be included in the map, then change one of the keys: + + ```dart + const a = 1; + const b = 2; + var m = {a: 'a', b: 'b'}; + ``` + + If only one of the entries is needed, then remove the one that isn't + needed: + + ```dart + const a = 1; + var m = {a: 'a'}; + ``` + + Note that literal maps preserve the order of their entries, so the choice + of which entry to remove might affect the order in which the keys and + values are returned by an iterator. SDK_VERSION_ASYNC_EXPORTED_FROM_CORE: problemMessage: "The class '{0}' wasn't exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions." correctionMessage: "Try either importing 'dart:async' or updating the SDK constraints." diff --git a/pkg/analyzer/test/src/diagnostics/equal_elements_in_const_set_test.dart b/pkg/analyzer/test/src/diagnostics/equal_elements_in_const_set_test.dart index 909d3c113deb..08e932285fad 100644 --- a/pkg/analyzer/test/src/diagnostics/equal_elements_in_const_set_test.dart +++ b/pkg/analyzer/test/src/diagnostics/equal_elements_in_const_set_test.dart @@ -143,7 +143,7 @@ var c = const {1, ...{1}}; await assertErrorsInCode(''' var c = {1, 2, 1}; ''', [ - error(HintCode.EQUAL_ELEMENTS_IN_SET, 15, 1), + error(WarningCode.EQUAL_ELEMENTS_IN_SET, 15, 1), ]); } } diff --git a/pkg/analyzer/test/src/diagnostics/equal_elements_in_set_test.dart b/pkg/analyzer/test/src/diagnostics/equal_elements_in_set_test.dart index cf24d77b4e54..d49d76d431aa 100644 --- a/pkg/analyzer/test/src/diagnostics/equal_elements_in_set_test.dart +++ b/pkg/analyzer/test/src/diagnostics/equal_elements_in_set_test.dart @@ -2,7 +2,7 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -import 'package:analyzer/src/dart/error/hint_codes.dart'; +import 'package:analyzer/src/error/codes.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; @@ -21,7 +21,7 @@ const a = 1; const b = 1; var s = {a, b}; ''', [ - error(HintCode.EQUAL_ELEMENTS_IN_SET, 38, 1), + error(WarningCode.EQUAL_ELEMENTS_IN_SET, 38, 1), ]); } @@ -30,7 +30,7 @@ var s = {a, b}; const one = 1; var s = {1, one}; ''', [ - error(HintCode.EQUAL_ELEMENTS_IN_SET, 27, 3), + error(WarningCode.EQUAL_ELEMENTS_IN_SET, 27, 3), ]); } @@ -38,7 +38,7 @@ var s = {1, one}; await assertErrorsInCode(''' var s = {1, 1}; ''', [ - error(HintCode.EQUAL_ELEMENTS_IN_SET, 12, 1), + error(WarningCode.EQUAL_ELEMENTS_IN_SET, 12, 1), ]); } } diff --git a/pkg/analyzer/test/src/diagnostics/equal_keys_in_const_map_test.dart b/pkg/analyzer/test/src/diagnostics/equal_keys_in_const_map_test.dart index 56a7275c03b5..51945735fafd 100644 --- a/pkg/analyzer/test/src/diagnostics/equal_keys_in_const_map_test.dart +++ b/pkg/analyzer/test/src/diagnostics/equal_keys_in_const_map_test.dart @@ -143,7 +143,7 @@ var c = const {1: null, ...{1: null}}; await assertErrorsInCode(''' var c = {1: null, 2: null, 1: null}; ''', [ - error(HintCode.EQUAL_KEYS_IN_MAP, 27, 1), + error(WarningCode.EQUAL_KEYS_IN_MAP, 27, 1), ]); } } diff --git a/pkg/analyzer/test/src/diagnostics/equal_keys_in_map_test.dart b/pkg/analyzer/test/src/diagnostics/equal_keys_in_map_test.dart index 176392239136..47df0dda5afb 100644 --- a/pkg/analyzer/test/src/diagnostics/equal_keys_in_map_test.dart +++ b/pkg/analyzer/test/src/diagnostics/equal_keys_in_map_test.dart @@ -2,7 +2,7 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -import 'package:analyzer/src/dart/error/hint_codes.dart'; +import 'package:analyzer/src/error/codes.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; @@ -21,7 +21,7 @@ const a = 1; const b = 1; var s = {a: 2, b: 3}; ''', [ - error(HintCode.EQUAL_KEYS_IN_MAP, 41, 1), + error(WarningCode.EQUAL_KEYS_IN_MAP, 41, 1), ]); } @@ -30,7 +30,7 @@ var s = {a: 2, b: 3}; const one = 1; var s = {1: 2, one: 3}; ''', [ - error(HintCode.EQUAL_KEYS_IN_MAP, 30, 3), + error(WarningCode.EQUAL_KEYS_IN_MAP, 30, 3), ]); } @@ -38,7 +38,7 @@ var s = {1: 2, one: 3}; await assertErrorsInCode(''' var s = {1: 2, 1: 3}; ''', [ - error(HintCode.EQUAL_KEYS_IN_MAP, 15, 1), + error(WarningCode.EQUAL_KEYS_IN_MAP, 15, 1), ]); } }