Skip to content

Commit 7341ad2

Browse files
srawlinsCommit Queue
authored and
Commit Queue
committed
Document unreachable_from_main for instance members
Change-Id: I37ea317ad0e1fc18d509e706a3cbc355f04005be Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/330380 Auto-Submit: Samuel Rawlins <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 44f99e1 commit 7341ad2

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

pkg/linter/lib/src/rules/unreachable_from_main.dart

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,13 @@ import '../analyzer.dart';
1616
const _desc = 'Unreachable top-level members in executable libraries.';
1717

1818
const _details = r'''
19-
Top-level members and static members in an executable library should be used
20-
'''
21-
// TODO(srawlins): Lasse
22-
// [suggests](https://github.com/dart-lang/linter/issues/3625#issuecomment-1735355630)
23-
// changing this to use "statically resolved" members, which I love. But it
24-
// will mean reporting additionally on extension instance members and extension
25-
// type instance members, so land carefully.
26-
'''
27-
directly inside this library. An executable library is a library that contains
28-
a `main` top-level function or that contains a top-level function annotated with
19+
Any member declared in an executable library should be used directly inside that
20+
library. An executable library is a library that contains a `main` top-level
21+
function or that contains a top-level function annotated with
2922
`@pragma('vm:entry-point')`). Executable libraries are not usually imported
3023
and it's better to avoid defining unused members.
3124
32-
This rule assumes that an executable library isn't imported by other files
25+
This rule assumes that an executable library isn't imported by other libraries
3326
except to execute its `main` function.
3427
3528
**BAD:**

pkg/linter/test/rules/unreachable_from_main_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,35 @@ extension type E(int it) {}
10201020
''');
10211021
}
10221022

1023+
test_instanceFieldOnExtension_unreachable() async {
1024+
await assertDiagnostics(r'''
1025+
void main() {
1026+
E.f;
1027+
}
1028+
1029+
extension E on int {
1030+
static int f = 1;
1031+
void m() {}
1032+
}
1033+
''', [
1034+
lint(72, 1),
1035+
]);
1036+
}
1037+
1038+
test_instanceMethod_unreachable_inExtensionType() async {
1039+
await assertDiagnostics(r'''
1040+
void main() {
1041+
E(7);
1042+
}
1043+
1044+
extension type E(int it) {
1045+
void m() {}
1046+
}
1047+
''', [
1048+
lint(59, 1),
1049+
]);
1050+
}
1051+
10231052
test_mixin_reachable_implemented() async {
10241053
await assertNoDiagnostics(r'''
10251054
void main() {

pkg/linter/tool/machine/rules.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,7 @@
384384
"group": "errors",
385385
"state": "stable",
386386
"incompatible": [],
387-
"sets": [
388-
"core",
389-
"recommended",
390-
"flutter"
391-
],
387+
"sets": [],
392388
"fixStatus": "needsEvaluation",
393389
"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",
394390
"sinceDartSdk": "3.1.0"
@@ -2646,7 +2642,7 @@
26462642
"incompatible": [],
26472643
"sets": [],
26482644
"fixStatus": "hasFix",
2649-
"details": "Top-level members and static members in an executable library should be used\ndirectly inside this library. An executable library is a library that contains\na `main` top-level function or that contains a top-level function annotated with\n`@pragma('vm:entry-point')`). Executable libraries are not usually imported\nand it's better to avoid defining unused members.\n\nThis rule assumes that an executable library isn't imported by other files\nexcept to execute its `main` function.\n\n**BAD:**\n\n```dart\nmain() {}\nvoid f() {}\n```\n\n**GOOD:**\n\n```dart\nmain() {\n f();\n}\nvoid f() {}\n```\n\n",
2645+
"details": "Any member declared in an executable library should be used directly inside that\nlibrary. An executable library is a library that contains a `main` top-level\nfunction or that contains a top-level function annotated with\n`@pragma('vm:entry-point')`). Executable libraries are not usually imported\nand it's better to avoid defining unused members.\n\nThis rule assumes that an executable library isn't imported by other libraries\nexcept to execute its `main` function.\n\n**BAD:**\n\n```dart\nmain() {}\nvoid f() {}\n```\n\n**GOOD:**\n\n```dart\nmain() {\n f();\n}\nvoid f() {}\n```\n\n",
26502646
"sinceDartSdk": "2.19.0"
26512647
},
26522648
{

0 commit comments

Comments
 (0)