Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit 8173b0f

Browse files
authored
chore: change unnecessary nullable parameters (#875)
* feat: introduce new command check-unnecessary-nullable * chore: update changelog * fix: add support for property access * fix: add support for index expressions * fix: add support for all expressions as arguments * fix: add null literal check * chore: remove unnecessary nullable parameters
1 parent b4bc57b commit 8173b0f

File tree

9 files changed

+27
-35
lines changed

9 files changed

+27
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* fix: correctly handle `-` symbol for [`prefer-commenting-analyzer-ignores`](https://dartcodemetrics.dev/docs/rules/common/prefer-commenting-analyzer-ignores).
1313
* fix: change elements equality check to overcome incorrect libs resolution.
1414
* chore: restrict `analyzer` version to `>=2.4.0 <4.2.0`.
15+
* chore: clean up unnecessary nullable parameters.
1516
* test: added test case in [`prefer-const-border-radius`](https://dartcodemetrics.dev/docs/rules/flutter/prefer-const-border-radius) rule.
1617

1718
## 4.15.2

lib/src/analyzers/lint_analyzer/metrics/scope_utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import '../models/scoped_function_declaration.dart';
44

55
/// Returns functions belonging to the passed [classNode]
66
Iterable<ScopedFunctionDeclaration> classMethods(
7-
AstNode? classNode,
7+
AstNode classNode,
88
Iterable<ScopedFunctionDeclaration> functionDeclarations,
99
) =>
1010
functionDeclarations

lib/src/analyzers/lint_analyzer/models/suppression.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Suppression {
1414
bool isSuppressed(String id) => _ignoreForFileSet.contains(_canonicalize(id));
1515

1616
/// Checks that the [id] is suppressed for the [lineIndex].
17-
bool isSuppressedAt(String id, int? lineIndex) =>
17+
bool isSuppressedAt(String id, int lineIndex) =>
1818
isSuppressed(id) ||
1919
(_ignoreMap[lineIndex]?.contains(_canonicalize(id)) ?? false);
2020

lib/src/analyzers/lint_analyzer/rules/base_visitors/intl_base_visitor.dart

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,13 @@ abstract class IntlBaseVisitor extends GeneralizingAstVisitor<void> {
2020
Iterable<IntlBaseIssue> get issues => _issues;
2121

2222
@protected
23-
void addIssue(IntlBaseIssue? issue) {
24-
if (issue != null) {
25-
_issues.add(issue);
26-
}
23+
void addIssue(IntlBaseIssue issue) {
24+
_issues.add(issue);
2725
}
2826

2927
@protected
30-
void addIssues(Iterable<IntlBaseIssue>? issues) {
31-
if (issues != null) {
32-
_issues.addAll(issues);
33-
}
28+
void addIssues(Iterable<IntlBaseIssue> issues) {
29+
_issues.addAll(issues);
3430
}
3531

3632
@override
@@ -98,8 +94,8 @@ abstract class IntlBaseVisitor extends GeneralizingAstVisitor<void> {
9894
@protected
9995
void checkMethodInvocation(
10096
MethodInvocation methodInvocation, {
97+
required String variableName,
10198
String? className,
102-
String? variableName,
10399
FormalParameterList? parameterList,
104100
});
105101

@@ -141,8 +137,8 @@ abstract class IntlBaseVisitor extends GeneralizingAstVisitor<void> {
141137

142138
void _checkMethodInvocation(
143139
MethodInvocation methodInvocation, {
140+
required String variableName,
144141
String? className,
145-
String? variableName,
146142
FormalParameterList? parameterList,
147143
}) {
148144
if ((methodInvocation.target?.as<SimpleIdentifier>()?.name != 'Intl') ||

lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_non_null_assertion/visitor.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,5 @@ class _Visitor extends RecursiveAstVisitor<void> {
2727
return false;
2828
}
2929

30-
bool _isMapOrSubclassOfMap(DartType? type) =>
31-
type != null && type.isDartCoreMap;
30+
bool _isMapOrSubclassOfMap(DartType type) => type.isDartCoreMap;
3231
}

lib/src/reporters/github_workflow_commands.dart

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,27 @@ class GitHubWorkflowCommands {
3636
);
3737

3838
String _construct(
39-
String command, [
40-
String? message,
41-
Map<String, Object>? parameters,
42-
]) {
39+
String command,
40+
String message,
41+
Map<String, Object> parameters,
42+
) {
4343
final buffer = StringBuffer('::$command');
4444
final params =
45-
parameters?.entries.map((e) => '${e.key}=${e.value}').join(',').trim();
46-
if (params != null && params.isNotEmpty) {
45+
parameters.entries.map((e) => '${e.key}=${e.value}').join(',').trim();
46+
if (params.isNotEmpty) {
4747
buffer.write(' $params');
4848
}
49-
buffer.write('::');
50-
if (message != null) {
51-
buffer.write(message);
52-
}
49+
50+
buffer
51+
..write('::')
52+
..write(message);
5353

5454
return buffer.toString();
5555
}
5656

57-
Map<String, Object> _params(String? file, int? line, int? column) => {
58-
if (file != null) 'file': file,
59-
if (line != null) 'line': line,
60-
if (column != null) 'col': column,
57+
Map<String, Object> _params(String file, int line, int column) => {
58+
'file': file,
59+
'line': line,
60+
'col': column,
6161
};
6262
}

lib/src/utils/exclude_utils.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import 'package:glob/glob.dart';
22
import 'package:path/path.dart' as p;
33

4-
bool isExcluded(String? absolutePath, Iterable<Glob> excludes) {
5-
final path = absolutePath?.replaceAll(r'\', '/');
4+
bool isExcluded(String absolutePath, Iterable<Glob> excludes) {
5+
final path = absolutePath.replaceAll(r'\', '/');
66

7-
return path != null && excludes.any((exclude) => exclude.matches(path));
7+
return excludes.any((exclude) => exclude.matches(path));
88
}
99

1010
Iterable<Glob> prepareExcludes(

test/src/analyzers/lint_analyzer/models/suppression_test.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ void main() {
1919
expect(suppression.isSuppressed('rule_id3'), isTrue);
2020
expect(suppression.isSuppressed('rule_id4'), isFalse);
2121

22-
expect(suppression.isSuppressedAt('rule_id1', null), isTrue);
23-
expect(suppression.isSuppressedAt('rule_id2', null), isTrue);
24-
expect(suppression.isSuppressedAt('rule_id3', null), isTrue);
2522
expect(suppression.isSuppressedAt('rule_id4', 5), isTrue);
2623
expect(suppression.isSuppressedAt('rule_id5', 5), isTrue);
2724
expect(suppression.isSuppressedAt('rule_id6', 9), isTrue);

test/src/analyzers/utils/scope_utils_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ void main() {
5050
),
5151
];
5252

53-
expect(classMethods(null, functions), hasLength(1));
5453
expect(classMethods(firstClass.declaration, functions), hasLength(2));
5554
expect(classMethods(secondClass.declaration, functions), hasLength(2));
5655
expect(classMethods(thirdClass.declaration, functions), isEmpty);

0 commit comments

Comments
 (0)