Skip to content

add unnecessary_underscores to recommended #863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/lints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
strategy:
fail-fast: false
matrix:
sdk: [beta] # todo: re-add stable
sdk: [dev, beta, stable]

steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
Expand All @@ -36,6 +36,7 @@ jobs:

- run: dart pub get
- run: dart format --output=none --set-exit-if-changed .
if: ${{ matrix.sdk == 'stable' }}
- run: dart analyze --fatal-infos
- run: dart tool/validate_lib.dart
- run: dart tool/gen_docs.dart --verify
6 changes: 6 additions & 0 deletions pkgs/lints/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 6.0.0-wip

- `recommended`:
- added [unnecessary_underscores] (https://github.com/dart-lang/core/issues/856)
- Require Dart 3.7.

## 5.1.1

- Updated the SDK lower bound to 3.6.
Expand Down
1 change: 1 addition & 0 deletions pkgs/lints/lib/recommended.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ linter:
- unnecessary_string_interpolations
- unnecessary_this
- unnecessary_to_list_in_spreads
- unnecessary_underscores
- use_function_type_syntax_for_parameters
- use_rethrow_when_possible
- use_super_parameters
4 changes: 2 additions & 2 deletions pkgs/lints/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: lints
version: 5.1.1
version: 6.0.0-wip
description: >
Official Dart lint rules. Defines the 'core' and 'recommended' set of lints
suggested by the Dart team.
Expand All @@ -11,7 +11,7 @@ topics:
- lints

environment:
sdk: ^3.6.0
sdk: ^3.7.0

# NOTE: Code is not allowed in this package - do not add dependencies.
# dependencies:
Expand Down
1 change: 1 addition & 0 deletions pkgs/lints/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
| [`unnecessary_string_interpolations`](https://dart.dev/lints/unnecessary_string_interpolations) | Unnecessary string interpolation. | ✅ |
| [`unnecessary_this`](https://dart.dev/lints/unnecessary_this) | Don't access members with `this` unless avoiding shadowing. | ✅ |
| [`unnecessary_to_list_in_spreads`](https://dart.dev/lints/unnecessary_to_list_in_spreads) | Unnecessary `toList()` in spreads. | ✅ |
| [`unnecessary_underscores`](https://dart.dev/lints/unnecessary_underscores) | Unnecessary underscores can be removed. | ✅ |
| [`use_function_type_syntax_for_parameters`](https://dart.dev/lints/use_function_type_syntax_for_parameters) | Use generic function type syntax for parameters. | ✅ |
| [`use_rethrow_when_possible`](https://dart.dev/lints/use_rethrow_when_possible) | Use rethrow to rethrow a caught exception. | ✅ |
| [`use_super_parameters`](https://dart.dev/lints/use_super_parameters) | Use super-initializer parameters where possible. | ✅ |
Expand Down
52 changes: 34 additions & 18 deletions pkgs/lints/tool/gen_docs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ void main(List<String> args) async {
}

// Generate new documentation.
var newRulesMarkdownContent =
_updateMarkdown(rulesMarkdownContent, rulesJson);
var newRulesMarkdownContent = _updateMarkdown(
rulesMarkdownContent,
rulesJson,
);

// If no documentation change, all is up-to-date.
if (newRulesMarkdownContent == rulesMarkdownContent) {
Expand All @@ -59,8 +61,10 @@ void main(List<String> args) async {

/// Documentation has changed.
if (verifyOnly) {
print('${rulesMarkdownFile.path} is not up-to-date (lint tables need to be '
'regenerated).');
print(
'${rulesMarkdownFile.path} is not up-to-date (lint tables need to be '
'regenerated).',
);
print('');
print("Run 'dart tool/gen_docs.dart' to re-generate.");
exit(1);
Expand All @@ -79,8 +83,9 @@ void main(List<String> args) async {
///
/// If [verifyOnly] is `true`, only reads the cached data back from
/// [rulesCacheFilePath].
Future<Map<String, Map<String, String>>> _fetchRulesJson(
{required bool verifyOnly}) async {
Future<Map<String, Map<String, String>>> _fetchRulesJson({
required bool verifyOnly,
}) async {
final rulesJsonFile = _packageRelativeFile(rulesCacheFilePath);
if (verifyOnly) {
final rulesJsonText = rulesJsonFile.readAsStringSync();
Expand All @@ -91,8 +96,9 @@ Future<Map<String, Map<String, String>>> _fetchRulesJson(

// Re-save [rulesJsonFile] file.
var newRulesJson = [...rulesJson.values];
rulesJsonFile
.writeAsStringSync(JsonEncoder.withIndent(' ').convert(newRulesJson));
rulesJsonFile.writeAsStringSync(
JsonEncoder.withIndent(' ').convert(newRulesJson),
);

return rulesJson;
}
Expand All @@ -108,8 +114,8 @@ Map<String, Map<String, String>> _readJson(String rulesJsonText) {
return {
for (Map<String, Object?> rule in rulesJson)
rule['name'] as String: {
for (var key in relevantKeys) key: rule[key] as String
}
for (var key in relevantKeys) key: rule[key] as String,
},
};
}

Expand All @@ -121,7 +127,9 @@ Map<String, Map<String, String>> _readJson(String rulesJsonText) {
/// [rulesJson], based on the list of rules in `lib/core.yaml` and
/// `lib/recommended.yaml`.
String _updateMarkdown(
String content, Map<String, Map<String, String>> rulesJson) {
String content,
Map<String, Map<String, String>> rulesJson,
) {
for (var ruleSetName in ['core', 'recommended']) {
var ruleFile = _packageRelativeFile(p.join('lib', '$ruleSetName.yaml'));
var ruleSet = _parseRules(ruleFile);
Expand All @@ -134,7 +142,10 @@ String _updateMarkdown(
continue;
}
content = content.replaceRange(
rangeStart, rangeEnd, _createRuleTable(ruleSet, rulesJson));
rangeStart,
rangeEnd,
_createRuleTable(ruleSet, rulesJson),
);
}
return content;
}
Expand All @@ -148,7 +159,9 @@ List<String> _parseRules(File yamlFile) {

/// Creates markdown source for a table of lint rules.
String _createRuleTable(
List<String> rules, Map<String, Map<String, String>> lintMeta) {
List<String> rules,
Map<String, Map<String, String>> lintMeta,
) {
rules.sort();

final lines = [
Expand All @@ -166,15 +179,18 @@ String _createRuleTable(
/// The row should have the same number of entires as the table format,
/// and should be on a single line with no newline at the end.
String _createRuleTableRow(
String rule, Map<String, Map<String, String>> lintMeta) {
String rule,
Map<String, Map<String, String>> lintMeta,
) {
final ruleMeta = lintMeta[rule];
if (ruleMeta == null) {
stderr.writeln("WARNING: Missing rule information for rule: $rule");
}
final description = (ruleMeta?['description'] ?? '')
.replaceAll('\n', ' ')
.replaceAll(RegExp(r'\s+'), ' ')
.trim();
final description =
(ruleMeta?['description'] ?? '')
.replaceAll('\n', ' ')
.replaceAll(RegExp(r'\s+'), ' ')
.trim();
final hasFix = ruleMeta?['fixStatus'] == 'hasFix';
final fixDesc = hasFix ? '✅' : '';

Expand Down
20 changes: 20 additions & 0 deletions pkgs/lints/tool/rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,11 @@
"description": "Specify non-obvious type annotations for top-level and static variables.",
"fixStatus": "hasFix"
},
{
"name": "strict_top_level_inference",
"description": "Specify type annotations.",
"fixStatus": "hasFix"
},
{
"name": "super_goes_last",
"description": "Place the `super` call last in a constructor initialization list.",
Expand Down Expand Up @@ -949,6 +954,11 @@
"description": "Use of angle brackets in a doc comment is treated as HTML by Markdown.",
"fixStatus": "needsFix"
},
{
"name": "unnecessary_async",
"description": "No await no async.",
"fixStatus": "needsFix"
},
{
"name": "unnecessary_await_in_return",
"description": "Unnecessary `await` keyword in return.",
Expand Down Expand Up @@ -984,6 +994,11 @@
"description": "Avoid wrapping fields in getters and setters just to be \"safe\".",
"fixStatus": "hasFix"
},
{
"name": "unnecessary_ignore",
"description": "Don't ignore a diagnostic code that is not produced.",
"fixStatus": "hasFix"
},
{
"name": "unnecessary_lambdas",
"description": "Don't create a lambda when a tear-off will do.",
Expand Down Expand Up @@ -1074,6 +1089,11 @@
"description": "Unnecessary `toList()` in spreads.",
"fixStatus": "hasFix"
},
{
"name": "unnecessary_underscores",
"description": "Unnecessary underscores can be removed.",
"fixStatus": "hasFix"
},
{
"name": "unreachable_from_main",
"description": "Unreachable top-level members in executable libraries.",
Expand Down
11 changes: 6 additions & 5 deletions pkgs/lints/tool/validate_lib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import 'package:path/path.dart' as p;
void main(List<String> args) {
print('Validating that there are no .dart source files in lib/ ...');

final dartSourceFiles = Directory('lib')
.listSync(recursive: true)
.whereType<File>()
.where((file) => p.extension(file.path) == '.dart')
.toList();
final dartSourceFiles =
Directory('lib')
.listSync(recursive: true)
.whereType<File>()
.where((file) => p.extension(file.path) == '.dart')
.toList();

if (dartSourceFiles.isEmpty) {
print('No Dart files found.');
Expand Down