Skip to content

Commit 9a850bf

Browse files
authored
Update diagnostic messages and linter rules (#5664)
These changes have been reviewed already in the SDK. Closes #5598
1 parent 3ab543f commit 9a850bf

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

src/_data/linter_rules.json

+26-11
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
"sets": [],
124124
"fixStatus": "noFix",
125125
"details": "This rule has been removed.\n",
126-
"sinceDartSdk": "3.3.0-wip"
126+
"sinceDartSdk": "3.3.0"
127127
},
128128
{
129129
"name": "avoid_web_libraries_in_flutter",
@@ -328,6 +328,17 @@
328328
"details": "**DON'T** test for conditions composed only by literals, since the value can be\ninferred at compile time.\n\nConditional statements using a condition which cannot be anything but FALSE have\nthe effect of making blocks of code non-functional. If the condition cannot\nevaluate to anything but `true`, the conditional statement is completely\nredundant, and makes the code less readable.\nIt is quite likely that the code does not match the programmer's intent.\nEither the condition should be removed or it should be updated so that it does\nnot always evaluate to `true` or `false`.\n\n**BAD:**\n```dart\nvoid bad() {\n if (true) {} // LINT\n}\n```\n\n**BAD:**\n```dart\nvoid bad() {\n if (true && 1 != 0) {} // LINT\n}\n```\n\n**BAD:**\n```dart\nvoid bad() {\n if (1 != 0 && true) {} // LINT\n}\n```\n\n**BAD:**\n```dart\nvoid bad() {\n if (1 < 0 && true) {} // LINT\n}\n```\n\n**BAD:**\n```dart\nvoid bad() {\n if (true && false) {} // LINT\n}\n```\n\n**BAD:**\n```dart\nvoid bad() {\n if (1 != 0) {} // LINT\n}\n```\n\n**BAD:**\n```dart\nvoid bad() {\n if (true && 1 != 0 || 3 < 4) {} // LINT\n}\n```\n\n**BAD:**\n```dart\nvoid bad() {\n if (1 != 0 || 3 < 4 && true) {} // LINT\n}\n```\n\n**NOTE:** that an exception is made for the common `while (true) { }` idiom,\nwhich is often reasonably preferred to the equivalent `for (;;)`.\n\n**GOOD:**\n```dart\nvoid good() {\n while (true) {\n // Do stuff.\n }\n}\n```\n",
329329
"sinceDartSdk": "2.0.0"
330330
},
331+
{
332+
"name": "missing_code_block_language_in_doc_comment",
333+
"description": "A code block is missing a specified language.",
334+
"group": "errors",
335+
"state": "stable",
336+
"incompatible": [],
337+
"sets": [],
338+
"fixStatus": "needsEvaluation",
339+
"details": "**DO** specify the language used in the code block of a doc comment.\n\nTo enable proper syntax highlighting of Markdown code blocks,\n[`dart doc`](https://dart.dev/tools/dart-doc) strongly recommends code blocks to\nspecify the language used after the initial code fence.\n\nSee [highlight.js](https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md)\nfor the list of languages supported by `dart doc`.\n\n**BAD:**\n```dart\n/// ```\n/// void main() {}\n/// ```\nclass A {}\n```\n\n**GOOD:**\n```dart\n/// ```dart\n/// void main() {}\n/// ```\nclass A {}\n```\n\n",
340+
"sinceDartSdk": "3.4.0-wip"
341+
},
331342
{
332343
"name": "no_adjacent_strings_in_list",
333344
"description": "Don't use adjacent strings in list.",
@@ -384,7 +395,11 @@
384395
"group": "errors",
385396
"state": "stable",
386397
"incompatible": [],
387-
"sets": [],
398+
"sets": [
399+
"core",
400+
"recommended",
401+
"flutter"
402+
],
388403
"fixStatus": "needsEvaluation",
389404
"details": "**DON'T** use wildcard parameters or variables.\n\nWildcard parameters and local variables\n(e.g. underscore-only names like `_`, `__`, `___`, etc.) will\nbecome non-binding in a future version of the Dart language.\nAny existing code that uses wildcard parameters or variables will\nbreak. In anticipation of this change, and to make adoption easier,\nthis lint disallows wildcard and variable parameter uses.\n\n\n**BAD:**\n```dart\nvar _ = 1;\nprint(_); // LINT\n```\n\n```dart\nvoid f(int __) {\n print(__); // LINT multiple underscores too\n}\n```\n\n**GOOD:**\n```dart\nfor (var _ in [1, 2, 3]) count++;\n```\n\n```dart\nvar [a, _, b, _] = [1, 2, 3, 4];\n```\n",
390405
"sinceDartSdk": "3.1.0"
@@ -623,7 +638,7 @@
623638
],
624639
"sets": [],
625640
"fixStatus": "hasFix",
626-
"details": "From the [style guide for the flutter repo](https://flutter.dev/style-guide/):\n\n**DO** specify type annotations.\n\nAvoid `var` when specifying that a type is unknown and short-hands that elide\ntype annotations. Use `dynamic` if you are being explicit that the type is\nunknown. Use `Object` if you are being explicit that you want an object that\nimplements `==` and `hashCode`.\n\n**BAD:**\n```dart\nvar foo = 10;\nfinal bar = Bar();\nconst quux = 20;\n```\n\n**GOOD:**\n```dart\nint foo = 10;\nfinal Bar bar = Bar();\nString baz = 'hello';\nconst int quux = 20;\n```\n\nNOTE: Using the the `@optionalTypeArgs` annotation in the `meta` package, API\nauthors can special-case type variables whose type needs to by dynamic but whose\ndeclaration should be treated as optional. For example, suppose you have a\n`Key` object whose type parameter you'd like to treat as optional. Using the\n`@optionalTypeArgs` would look like this:\n\n```dart\nimport 'package:meta/meta.dart';\n\n@optionalTypeArgs\nclass Key<T> {\n ...\n}\n\nmain() {\n Key s = Key(); // OK!\n}\n```\n\n",
641+
"details": "From the [style guide for the flutter repo](https://flutter.dev/style-guide/):\n\n**DO** specify type annotations.\n\nAvoid `var` when specifying that a type is unknown and short-hands that elide\ntype annotations. Use `dynamic` if you are being explicit that the type is\nunknown. Use `Object` if you are being explicit that you want an object that\nimplements `==` and `hashCode`.\n\n**BAD:**\n```dart\nvar foo = 10;\nfinal bar = Bar();\nconst quux = 20;\n```\n\n**GOOD:**\n```dart\nint foo = 10;\nfinal Bar bar = Bar();\nString baz = 'hello';\nconst int quux = 20;\n```\n\nNOTE: Using the the `@optionalTypeArgs` annotation in the `meta` package, API\nauthors can special-case type variables whose type needs to be dynamic but whose\ndeclaration should be treated as optional. For example, suppose you have a\n`Key` object whose type parameter you'd like to treat as optional. Using the\n`@optionalTypeArgs` would look like this:\n\n```dart\nimport 'package:meta/meta.dart';\n\n@optionalTypeArgs\nclass Key<T> {\n ...\n}\n\nmain() {\n Key s = Key(); // OK!\n}\n```\n\n",
627642
"sinceDartSdk": "2.0.0"
628643
},
629644
{
@@ -1348,7 +1363,11 @@
13481363
"group": "style",
13491364
"state": "stable",
13501365
"incompatible": [],
1351-
"sets": [],
1366+
"sets": [
1367+
"core",
1368+
"recommended",
1369+
"flutter"
1370+
],
13521371
"fixStatus": "hasFix",
13531372
"details": "Attach library annotations to library directives, rather than\nsome other library-level element.\n\n**BAD:**\n```dart\n@TestOn('browser')\n\nimport 'package:test/test.dart';\n\nvoid main() {}\n```\n\n**GOOD:**\n```dart\n@TestOn('browser')\nlibrary;\n\nimport 'package:test/test.dart';\n\nvoid main() {}\n```\n\n**NOTE:** An unnamed library, like `library;` above, is only supported in Dart\n2.19 and later. Code which might run in earlier versions of Dart will need to\nprovide a name in the `library` directive.\n",
13541373
"sinceDartSdk": "2.19.0"
@@ -1610,11 +1629,7 @@
16101629
"group": "style",
16111630
"state": "stable",
16121631
"incompatible": [],
1613-
"sets": [
1614-
"core",
1615-
"recommended",
1616-
"flutter"
1617-
],
1632+
"sets": [],
16181633
"fixStatus": "noFix",
16191634
"details": "**DO** prefix library names with the package name and a dot-separated path.\n\nThis guideline helps avoid the warnings you get when two libraries have the same\nname. Here are the rules we recommend:\n\n* Prefix all library names with the package name.\n* Make the entry library have the same name as the package.\n* For all other libraries in a package, after the package name add the\ndot-separated path to the library's Dart file.\n* For libraries under `lib`, omit the top directory name.\n\nFor example, say the package name is `my_package`. Here are the library names\nfor various files in the package:\n\n**GOOD:**\n```dart\n// In lib/my_package.dart\nlibrary my_package;\n\n// In lib/other.dart\nlibrary my_package.other;\n\n// In lib/foo/bar.dart\nlibrary my_package.foo.bar;\n\n// In example/foo/bar.dart\nlibrary my_package.example.foo.bar;\n\n// In lib/src/private.dart\nlibrary my_package.src.private;\n```\n\n",
16201635
"sinceDartSdk": "2.0.0"
@@ -1792,7 +1807,7 @@
17921807
],
17931808
"sets": [],
17941809
"fixStatus": "hasFix",
1795-
"details": "**DO** use double quotes where they wouldn't require additional escapes.\n\nThat means strings with a double quote may use apostrophes so that the double\nquote isn't escaped (note: we don't lint the other way around, ie, a double\nquoted string with an escaped double quote is not flagged).\n\nIt's also rare, but possible, to have strings within string interpolations. In\nthis case, its much more readable to use a single quote somewhere. So single\nquotes are allowed either within, or containing, an interpolated string literal.\nArguably strings within string interpolations should be its own type of lint.\n\n**BAD:**\n```dart\nuseStrings(\n 'should be double quote',\n r'should be double quote',\n r'''should be double quotes''')\n```\n\n**GOOD:**\n```dart\nuseStrings(\n \"should be double quote\",\n r\"should be double quote\",\n r\"\"\"should be double quotes\"\"\",\n 'ok with \" inside',\n 'nested ${a ? \"strings\" : \"can\"} be wrapped by a double quote',\n \"and nested ${a ? 'strings' : 'can be double quoted themselves'}\");\n```\n\n",
1810+
"details": "**DO** use double quotes where they wouldn't require additional escapes.\n\nThat means strings with a double quote may use apostrophes so that the double\nquote isn't escaped (note: we don't lint the other way around, ie, a double\nquoted string with an escaped double quote is not flagged).\n\nIt's also rare, but possible, to have strings within string interpolations. In\nthis case, it's much more readable to use a single quote somewhere. So single\nquotes are allowed either within, or containing, an interpolated string literal.\nArguably strings within string interpolations should be its own type of lint.\n\n**BAD:**\n```dart\nuseStrings(\n 'should be double quote',\n r'should be double quote',\n r'''should be double quotes''')\n```\n\n**GOOD:**\n```dart\nuseStrings(\n \"should be double quote\",\n r\"should be double quote\",\n r\"\"\"should be double quotes\"\"\",\n 'ok with \" inside',\n 'nested ${a ? \"strings\" : \"can\"} be wrapped by a double quote',\n \"and nested ${a ? 'strings' : 'can be double quoted themselves'}\");\n```\n\n",
17961811
"sinceDartSdk": "2.4.0"
17971812
},
17981813
{
@@ -2106,7 +2121,7 @@
21062121
],
21072122
"sets": [],
21082123
"fixStatus": "hasFix",
2109-
"details": "**DO** use single quotes where they wouldn't require additional escapes.\n\nThat means strings with an apostrophe may use double quotes so that the\napostrophe isn't escaped (note: we don't lint the other way around, ie, a single\nquoted string with an escaped apostrophe is not flagged).\n\nIt's also rare, but possible, to have strings within string interpolations. In\nthis case, its much more readable to use a double quote somewhere. So double\nquotes are allowed either within, or containing, an interpolated string literal.\nArguably strings within string interpolations should be its own type of lint.\n\n**BAD:**\n```dart\nuseStrings(\n \"should be single quote\",\n r\"should be single quote\",\n r\"\"\"should be single quotes\"\"\")\n```\n\n**GOOD:**\n```dart\nuseStrings(\n 'should be single quote',\n r'should be single quote',\n r'''should be single quotes''',\n \"here's ok\",\n \"nested ${a ? 'strings' : 'can'} be wrapped by a double quote\",\n 'and nested ${a ? \"strings\" : \"can be double quoted themselves\"}');\n```\n\n",
2124+
"details": "**DO** use single quotes where they wouldn't require additional escapes.\n\nThat means strings with an apostrophe may use double quotes so that the\napostrophe isn't escaped (note: we don't lint the other way around, ie, a single\nquoted string with an escaped apostrophe is not flagged).\n\nIt's also rare, but possible, to have strings within string interpolations. In\nthis case, it's much more readable to use a double quote somewhere. So double\nquotes are allowed either within, or containing, an interpolated string literal.\nArguably strings within string interpolations should be its own type of lint.\n\n**BAD:**\n```dart\nuseStrings(\n \"should be single quote\",\n r\"should be single quote\",\n r\"\"\"should be single quotes\"\"\")\n```\n\n**GOOD:**\n```dart\nuseStrings(\n 'should be single quote',\n r'should be single quote',\n r'''should be single quotes''',\n \"here's ok\",\n \"nested ${a ? 'strings' : 'can'} be wrapped by a double quote\",\n 'and nested ${a ? \"strings\" : \"can be double quoted themselves\"}');\n```\n\n",
21102125
"sinceDartSdk": "2.0.0"
21112126
},
21122127
{

src/_data/side-nav.yml

+2
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@
337337
permalink: /tools/non-promotion-reasons
338338
- title: Linter rules
339339
permalink: /tools/linter-rules
340+
- title: Diagnostic messages
341+
permalink: /tools/diagnostic-messages
340342
- title: Testing & optimization
341343
children:
342344
- title: Testing

src/_data/site.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ og_image_vers: "?2"
3636

3737
sdkInfo:
3838
channel: stable
39-
version: 3.3.0
39+
version: 3.3.2

src/content/tools/diagnostic-messages.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4495,15 +4495,15 @@ The following code produces this diagnostic because the result of dividing
44954495
`x` and `y` is converted to an integer using `toInt`:
44964496

44974497
```dart
4498-
int divide(num x, num y) => [!(x / y).toInt()!];
4498+
int divide(int x, int y) => [!(x / y).toInt()!];
44994499
```
45004500

45014501
#### Common fixes
45024502

45034503
Use the integer division operator (`~/`):
45044504

45054505
```dart
4506-
int divide(num x, num y) => x ~/ y;
4506+
int divide(int x, int y) => x ~/ y;
45074507
```
45084508

45094509
### duplicate_constructor

0 commit comments

Comments
 (0)