[BUG]: check-unnecessary-nullable check false positive result in Tear-offs #1153
Description
- Dart code metrics version: 5.4.0
- Dart sdk version: 2.18.6
- Flutter version: 3.3.10 (stable)
Please show your full configuration:
Configuration
include: package:lint/analysis_options.yaml
linter:
rules:
constant_identifier_names: false
analyzer:
exclude:
- "**/*.g.dart"
- "**/*.freezed.dart"
errors:
invalid_annotation_target: ignore
use_super_parameters: ignore
What did you do? Please include the source code example causing the issue.
All I did was run CLI command to check for unnecessary nullable
dart run dart_code_metrics:metrics check-unnecessary-nullable lib
More Detail trying to make sense on why and what actually happened
So I have a function that validates a form and add/update an item in a list
bool validateAndAdd({int? indexOfItemToBeUpdated}) {
// some validation logic
}
Here, if the indexOfItemToBeUpdated
is null
then the item will be added else item at provided index will be updated with new values.
This is how these values are called/used
// For Add
openEditorDialog(
onSubmit: addEditController.validateAndAdd, // Notice this line
child: const AddEditView(),
);
// For Update
openEditorDialog(
onSubmit: () => addEditController.validateAndAdd(indexOfItemToBeUpdated: index),
child: const AddEditView(),
);
So what is the problem?
Running
dart run dart_code_metrics:metrics check-unnecessary-nullable lib
gives
lib/app/.../my_file_name.dart:
method validateAndAdd has unnecessary nullable parameters
(int? indexOfItemToBeUpdated)
at /Users/.../my_file_name.dart:46:3
Anything else?
I change how validateAndAdd
function was called for Add case
I just replaced Tear-off method of calling a function with equivalent expression function
// Using Tear-off (Old)
// openEditorDialog(
// onSubmit: addEditController.validateAndAdd,
// child: const AddEditView(),
// );
// Using expression function (New)
openEditorDialog(
onSubmit: () => addEditController.validateAndAdd(),
child: const AddEditView(),
);
and then I ran check-unnecessary-nullable
command once more.
Voila! dart_code_metrics
doesn't list this file anymore
Final Thought
I love using tear-offs and it would be great if dart_code_metrics supports them. This example provided above is just 1 of many places I have used tear-off and got this message. I know this might not be priority task compared to some other Open tickets and that is fine but please have a look at this when you get a chance.
Are you willing to submit a pull request to fix this bug?
No. I would love to help but I don't know where/how to start.