Skip to content

Commit 0840051

Browse files
bwilkersonCommit Queue
authored andcommitted
Add documentation for several additional warnings
Change-Id: If92e477d77c4bd3390ff1b2927ebaea1a76cfa73 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/444390 Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 456e81e commit 0840051

File tree

3 files changed

+293
-4
lines changed

3 files changed

+293
-4
lines changed

pkg/analyzer/lib/src/error/codes.g.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6747,10 +6747,11 @@ class WarningCode extends DiagnosticCode {
67476747
/// https://github.com/dart-lang/language/blob/master/resources/type-system/strict-inference.md
67486748
///
67496749
/// Parameters:
6750-
/// String p0: the name of the function or method
6750+
/// String p0: the name of the function or method whose return type couldn't
6751+
/// be inferred
67516752
static const WarningCode inferenceFailureOnFunctionReturnType = WarningCode(
67526753
'INFERENCE_FAILURE_ON_FUNCTION_RETURN_TYPE',
6753-
"The return type of '{0}' cannot be inferred.",
6754+
"The return type of '{0}' can't be inferred.",
67546755
correctionMessage: "Declare the return type of '{0}'.",
67556756
);
67566757

pkg/analyzer/messages.yaml

Lines changed: 287 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24663,6 +24663,45 @@ WarningCode:
2466324663
comment: |-
2466424664
When "strict-inference" is enabled, collection literal types must be
2466524665
inferred via the context type, or have type arguments.
24666+
documentation: |-
24667+
#### Description
24668+
24669+
The analyzer produces this diagnostic when
24670+
- the language option `strict-inference` has been enabled in the analysis options file,
24671+
- a list, map or set literal doesn't have type arguments, and
24672+
- the values for the type arguments can't be inferred from the elements.
24673+
24674+
#### Example
24675+
24676+
Given an analysis options file containing the following:
24677+
24678+
```yaml
24679+
%uri="analysis_options.yaml"
24680+
analyzer:
24681+
language:
24682+
strict-inference: true
24683+
```
24684+
24685+
The following code produces this diagnostic because the type of the
24686+
elements of the list literal can't be inferred by the analyzer:
24687+
24688+
```dart
24689+
void f() {
24690+
var list = [![]!];
24691+
print(list);
24692+
}
24693+
```
24694+
24695+
#### Common fixes
24696+
24697+
Provide explicit type arguments for the literal:
24698+
24699+
```dart
24700+
void f() {
24701+
var list = <int>[];
24702+
print(list);
24703+
}
24704+
```
2466624705
INFERENCE_FAILURE_ON_FUNCTION_INVOCATION:
2466724706
parameters:
2466824707
String p0: the name of the function
@@ -24672,10 +24711,56 @@ WarningCode:
2467224711
comment: |-
2467324712
When "strict-inference" is enabled, types in function invocations must be
2467424713
inferred via the context type, or have type arguments.
24714+
documentation: |-
24715+
#### Description
24716+
24717+
The analyzer produces this diagnostic when
24718+
- the language option `strict-inference` has been enabled in the analysis options file,
24719+
- the invocation of a method or function doesn't have type arguments, and
24720+
- the values for the type arguments can't be inferred.
24721+
24722+
#### Example
24723+
24724+
Given an analysis options file containing the following:
24725+
24726+
```yaml
24727+
%uri="analysis_options.yaml"
24728+
analyzer:
24729+
language:
24730+
strict-inference: true
24731+
```
24732+
24733+
The following code produces this diagnostic because the invocation of the
24734+
method `m` doesn't have type arguments and the type arguments can't be
24735+
inferred:
24736+
24737+
```dart
24738+
abstract class C {
24739+
void m<T>();
24740+
}
24741+
24742+
void f(C c) {
24743+
c.[!m!]();
24744+
}
24745+
```
24746+
24747+
#### Common fixes
24748+
24749+
Provide explicit type arguments for the invocation:
24750+
24751+
```dart
24752+
abstract class C {
24753+
void m<T>();
24754+
}
24755+
24756+
void f(C c) {
24757+
c.m<int>();
24758+
}
24759+
```
2467524760
INFERENCE_FAILURE_ON_FUNCTION_RETURN_TYPE:
2467624761
parameters:
24677-
String p0: the name of the function or method
24678-
problemMessage: "The return type of '#p0' cannot be inferred."
24762+
String p0: the name of the function or method whose return type couldn't be inferred
24763+
problemMessage: "The return type of '#p0' can't be inferred."
2467924764
correctionMessage: "Declare the return type of '#p0'."
2468024765
hasPublishedDocs: false
2468124766
comment: |-
@@ -24684,6 +24769,43 @@ WarningCode:
2468424769
specify a return type. See the strict-inference resource:
2468524770

2468624771
https://github.com/dart-lang/language/blob/master/resources/type-system/strict-inference.md
24772+
documentation: |-
24773+
#### Description
24774+
24775+
The analyzer produces this diagnostic when
24776+
- the language option `strict-inference` has been enabled in the analysis options file,
24777+
- the declaration of a method or function doesn't have a return type, and
24778+
- the return type can't be inferred.
24779+
24780+
#### Example
24781+
24782+
Given an analysis options file containing the following:
24783+
24784+
```yaml
24785+
%uri="analysis_options.yaml"
24786+
analyzer:
24787+
language:
24788+
strict-inference: true
24789+
```
24790+
24791+
The following code produces this diagnostic because the method `m` doesn't
24792+
have a return type:
24793+
24794+
```dart
24795+
class C {
24796+
[!m!]() => 7;
24797+
}
24798+
```
24799+
24800+
#### Common fixes
24801+
24802+
Add a return type to the method or function:
24803+
24804+
```dart
24805+
class C {
24806+
int m() => 7;
24807+
}
24808+
```
2468724809
INFERENCE_FAILURE_ON_GENERIC_INVOCATION:
2468824810
parameters:
2468924811
String p0: the name of the type
@@ -24712,6 +24834,40 @@ WarningCode:
2471224834
comment: |-
2471324835
When "strict-inference" in enabled, uninitialized variables must be
2471424836
declared with a specific type.
24837+
documentation: |-
24838+
#### Description
24839+
24840+
The analyzer produces this diagnostic when
24841+
- the language option `strict-inference` has been enabled in the analysis options file,
24842+
- the declaration of a variable doesn't have a type, and
24843+
- the type of the variable can't be inferred.
24844+
24845+
#### Example
24846+
24847+
Given an analysis options file containing the following:
24848+
24849+
```yaml
24850+
%uri="analysis_options.yaml"
24851+
analyzer:
24852+
language:
24853+
strict-inference: true
24854+
```
24855+
24856+
The following code produces this diagnostic because the variable `s`
24857+
doesn't have an explicit type and the type can't be inferred because
24858+
there's no initializer:
24859+
24860+
```dart
24861+
var [!s!];
24862+
```
24863+
24864+
#### Common fixes
24865+
24866+
Add an explicit type:
24867+
24868+
```dart
24869+
String? s;
24870+
```
2471524871
INFERENCE_FAILURE_ON_UNTYPED_PARAMETER:
2471624872
parameters:
2471724873
String p0: the name of the parameter
@@ -24721,6 +24877,42 @@ WarningCode:
2472124877
comment: |-
2472224878
When "strict-inference" in enabled, function parameters must be
2472324879
declared with a specific type, or inherit a type.
24880+
documentation: |-
24881+
#### Description
24882+
24883+
The analyzer produces this diagnostic when
24884+
- the language option `strict-inference` has been enabled in the analysis options file,
24885+
- the declaration of a formal parameter doesn't have a type, and
24886+
- the type of the parameter can't be inferred.
24887+
24888+
The type of a parameter of a method can be inferred if the method
24889+
overrides an inherited method.
24890+
24891+
#### Example
24892+
24893+
Given an analysis options file containing the following:
24894+
24895+
```yaml
24896+
%uri="analysis_options.yaml"
24897+
analyzer:
24898+
language:
24899+
strict-inference: true
24900+
```
24901+
24902+
The following code produces this diagnostic because the formal parameter
24903+
`p` doesn't have an explicit type and the type can't be inferred:
24904+
24905+
```dart
24906+
void f([!p!]) => print(p);
24907+
```
24908+
24909+
#### Common fixes
24910+
24911+
Add an explicit type:
24912+
24913+
```dart
24914+
void f(int p) => print(p);
24915+
```
2472424916
INVALID_ANNOTATION_TARGET:
2472524917
parameters:
2472624918
String p0: the name of the annotation
@@ -26820,6 +27012,39 @@ WarningCode:
2682027012
problemMessage: "The matched value type '#p0' can never match the required type '#p1'."
2682127013
correctionMessage: "Try using a different pattern."
2682227014
hasPublishedDocs: false
27015+
documentation: |-
27016+
#### Description
27017+
27018+
The analyzer produces this diagnostic when the type of the object being
27019+
matched can't ever be matched by the pattern.
27020+
27021+
#### Example
27022+
27023+
The following code produces this diagnostic because a `double` is being
27024+
matched by a pattern that requires an `int`, which can never succeed:
27025+
27026+
```dart
27027+
void f(String? s) {
27028+
if (s case [!int!] _) {}
27029+
}
27030+
```
27031+
27032+
#### Common fixes
27033+
27034+
If one of the types is wrong, then change one or both of the types so that
27035+
the pattern can succeed:
27036+
27037+
```dart
27038+
void f(String? s) {
27039+
if (s case String _) {}
27040+
}
27041+
```
27042+
27043+
If the types aren't wrong, then remove the pattern match:
27044+
27045+
```dart
27046+
void f(double x) {}
27047+
```
2682327048
RECEIVER_OF_TYPE_NEVER:
2682427049
parameters: none
2682527050
problemMessage: "The receiver is of type 'Never', and will never complete with a value."
@@ -26909,6 +27134,36 @@ WarningCode:
2690927134
hasPublishedDocs: false
2691027135
comment: |-
2691127136
An error code indicating use of a removed lint rule.
27137+
documentation: |-
27138+
#### Description
27139+
27140+
The analyzer produces this diagnostic when a lint that has been removed is
27141+
used in an analysis options file. Because the lint has been removed, the
27142+
reference to it (such as enabling it) will have no effect.
27143+
27144+
#### Example
27145+
27146+
Assuming that the lint `removed_lint` has been removed, the following
27147+
options file produces this diagnostic:
27148+
27149+
```yaml
27150+
%uri="analysis_options.yaml"
27151+
linter:
27152+
rules:
27153+
- always_put_required_named_parameters_first
27154+
- [!removed_lint!]
27155+
```
27156+
27157+
#### Common fixes
27158+
27159+
Remove the reference to the lint code:
27160+
27161+
```yaml
27162+
%uri="analysis_options.yaml"
27163+
linter:
27164+
rules:
27165+
- always_put_required_named_parameters_first
27166+
```
2691227167
REPLACED_LINT_USE:
2691327168
parameters:
2691427169
Object p0: the rule name
@@ -27980,6 +28235,36 @@ WarningCode:
2798028235
problemMessage: Unnecessary cast pattern.
2798128236
correctionMessage: Try removing the cast pattern.
2798228237
hasPublishedDocs: false
28238+
documentation: |-
28239+
#### Description
28240+
28241+
The analyzer produces this diagnostic when a cast pattern is used on a
28242+
value that is known to be of the specified type.
28243+
28244+
#### Example
28245+
28246+
The following code produces this diagnostic because the cast `as num` is
28247+
known to always succeed because the type of `z` is `int`:
28248+
28249+
```dart
28250+
void f(int x) {
28251+
if (x case var z [!as!] num) {
28252+
print(z);
28253+
}
28254+
}
28255+
```
28256+
28257+
#### Common fixes
28258+
28259+
Remove the cast pattern:
28260+
28261+
```dart
28262+
void f(int x) {
28263+
if (x case var z) {
28264+
print(z);
28265+
}
28266+
}
28267+
```
2798328268
UNNECESSARY_FINAL:
2798428269
parameters: none
2798528270
problemMessage: The keyword 'final' isn't necessary because the parameter is implicitly 'final'.

pkg/analyzer/test/verify_diagnostics_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ class DocumentationValidator {
181181

182182
// Reports CompileTimeErrorCode.FINAL_CLASS_EXTENDED_OUTSIDE_OF_LIBRARY
183183
'WarningCode.DEPRECATED_EXTENDS_FUNCTION',
184+
// Doesn't apply to Dart files.
185+
// TODO(brianwilkerson): Provide better support for non-Dart files.
186+
'WarningCode.REMOVED_LINT_USE',
184187
// Produces more than one error range by design.
185188
// TODO(srawlins): update verification to allow for multiple highlight ranges.
186189
'WarningCode.TEXT_DIRECTION_CODE_POINT_IN_COMMENT',

0 commit comments

Comments
 (0)