Skip to content

Commit

Permalink
[analyzer] Move two more Hints to be Warnings, EQUAL_*
Browse files Browse the repository at this point in the history
Bug: #50796

Change-Id: Id07ac5940eafed58b1473804248381dd933cb311
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279229
Commit-Queue: Samuel Rawlins <[email protected]>
Reviewed-by: Konstantin Shcheglov <[email protected]>
  • Loading branch information
srawlins authored and Commit Queue committed Jan 19, 2023
1 parent 4871e74 commit a4ca3de
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: |-
Expand Down Expand Up @@ -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:
Expand All @@ -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
notes: Deprecated
16 changes: 0 additions & 16 deletions pkg/analyzer/lib/src/dart/error/hint_codes.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/analyzer/lib/src/error/best_practices_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -990,8 +990,8 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
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);
}
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/analyzer/lib/src/error/codes.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions pkg/analyzer/lib/src/error/error_code_values.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,6 @@ const List<ErrorCode> 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,
Expand Down Expand Up @@ -972,6 +970,8 @@ const List<ErrorCode> 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,
Expand Down
182 changes: 91 additions & 91 deletions pkg/analyzer/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <int>{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 = <int>{a, b};
```

If only one of the elements is needed, then remove the one that isn't
needed:

```dart
const a = 1;
var s = <int>{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 = <int, String>{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 = <int, String>{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 = <int, String>{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."
Expand Down Expand Up @@ -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 = <int>{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 = <int>{a, b};
```

If only one of the elements is needed, then remove the one that isn't
needed:

```dart
const a = 1;
var s = <int>{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 = <int, String>{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 = <int, String>{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 = <int, String>{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."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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),
]);
}

Expand All @@ -30,15 +30,15 @@ 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),
]);
}

test_literal_literal() async {
await assertErrorsInCode('''
var s = {1, 1};
''', [
error(HintCode.EQUAL_ELEMENTS_IN_SET, 12, 1),
error(WarningCode.EQUAL_ELEMENTS_IN_SET, 12, 1),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);
}
}
8 changes: 4 additions & 4 deletions pkg/analyzer/test/src/diagnostics/equal_keys_in_map_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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),
]);
}

Expand All @@ -30,15 +30,15 @@ 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),
]);
}

test_literal_literal() async {
await assertErrorsInCode('''
var s = {1: 2, 1: 3};
''', [
error(HintCode.EQUAL_KEYS_IN_MAP, 15, 1),
error(WarningCode.EQUAL_KEYS_IN_MAP, 15, 1),
]);
}
}

0 comments on commit a4ca3de

Please sign in to comment.