-
Notifications
You must be signed in to change notification settings - Fork 25
migrated avoid non null assertion rule and tests #230
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
Islam-Shaaban-Ibrahim
merged 3 commits into
analysis_server_migration
from
migrate/avoid_non_null
Apr 16, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
lib/src/lints/avoid_non_null_assertion/visitors/avoid_non_null_assertion_visitor.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/token.dart'; | ||
| import 'package:analyzer/dart/ast/visitor.dart'; | ||
| import 'package:analyzer/dart/element/type.dart'; | ||
| import 'package:solid_lints/src/lints/avoid_non_null_assertion/avoid_non_null_assertion_rule.dart'; | ||
|
|
||
| /// visitor for [AvoidNonNullAssertionRule] | ||
| class AvoidNonNullAssertionVisitor extends SimpleAstVisitor<void> { | ||
| /// Rule associated with this visitor | ||
| final AvoidNonNullAssertionRule rule; | ||
|
|
||
| /// Creates an instance of [AvoidNonNullAssertionVisitor] | ||
| AvoidNonNullAssertionVisitor(this.rule); | ||
|
|
||
| @override | ||
| void visitPostfixExpression(PostfixExpression node) { | ||
| if (node.operator.type != TokenType.BANG) { | ||
| return; | ||
| } | ||
|
|
||
| final operand = node.operand; | ||
|
|
||
| if (operand is IndexExpression) { | ||
| final type = operand.target?.staticType; | ||
|
|
||
| if (_isMap(type)) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| rule.reportAtNode(node); | ||
| } | ||
|
|
||
| bool _isMap(DartType? type) { | ||
| if (type is! InterfaceType) { | ||
| return false; | ||
| } | ||
|
|
||
| return type.isDartCoreMap || type.allSupertypes.any((v) => v.isDartCoreMap); | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import 'package:analyzer_testing/analysis_rule/analysis_rule.dart'; | ||
| import 'package:solid_lints/src/lints/avoid_non_null_assertion/avoid_non_null_assertion_rule.dart'; | ||
| import 'package:test_reflective_loader/test_reflective_loader.dart'; | ||
|
|
||
| void main() { | ||
| defineReflectiveSuite(() { | ||
| defineReflectiveTests(AvoidNonNullAssertionRuleTest); | ||
| }); | ||
| } | ||
|
|
||
| @reflectiveTest | ||
| class AvoidNonNullAssertionRuleTest extends AnalysisRuleTest { | ||
| @override | ||
| void setUp() { | ||
| rule = AvoidNonNullAssertionRule(); | ||
| super.setUp(); | ||
| } | ||
|
|
||
| void test_reports_non_null_assertion_on_nullable_value() async { | ||
| await assertDiagnostics( | ||
| r''' | ||
| void m(int? number) { | ||
| final value = number!; | ||
| } | ||
| ''', | ||
| [lint(38, 7)], | ||
| ); | ||
| } | ||
|
|
||
| void test_reports_non_null_assertion_on_method_call() async { | ||
| await assertDiagnostics( | ||
| r''' | ||
| void m(Object? object) { | ||
| object!.toString(); | ||
| } | ||
| ''', | ||
| [lint(27, 7)], | ||
| ); | ||
| } | ||
|
|
||
| void test_does_not_report_map_access() async { | ||
| await assertNoDiagnostics(r''' | ||
| void m() { | ||
| final map = {'key': 'value'}; | ||
| map['key']!; | ||
| } | ||
| '''); | ||
| } | ||
|
|
||
| void test_does_not_report_safe_null_check() async { | ||
| await assertNoDiagnostics(r''' | ||
| void m(int? number) { | ||
| if (number != null) { | ||
| final value = number; | ||
| } | ||
| } | ||
| '''); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.