diff --git a/CHANGELOG.md b/CHANGELOG.md index 39edb30bb0..8e30c49010 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* Don't trigger prefer-extracting-callbacks on empty function blocks. * Improve unused files check, add support for `vm:entry-point` annotation. ## 4.3.1 diff --git a/doc/rules/prefer-extracting-callbacks.md b/doc/rules/prefer-extracting-callbacks.md index 7dd202acbf..b3d0334e88 100644 --- a/doc/rules/prefer-extracting-callbacks.md +++ b/doc/rules/prefer-extracting-callbacks.md @@ -10,7 +10,9 @@ prefer-extracting-callbacks Warns about inline callbacks in a widget tree and suggests to extract them to widget methods in order to make a `build` method more readable. In addition extracting can help test those methods separately as well. -**NOTE** the rule will not trigger on arrow functions like `onPressed: () => _handler(...)` in order to cover cases when a callback needs a variable from the outside. +**NOTE** the rule will not trigger on: + - arrow functions like `onPressed: () => _handler(...)` in order to cover cases when a callback needs a variable from the outside; + - empty blocks. Use `ignored-named-arguments` configuration, if you want to ignore specific named parameters (`builder` argument is ignored by default). diff --git a/lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_extracting_callbacks/visitor.dart b/lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_extracting_callbacks/visitor.dart index cdf9d64460..21946211ef 100644 --- a/lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_extracting_callbacks/visitor.dart +++ b/lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_extracting_callbacks/visitor.dart @@ -40,12 +40,21 @@ class _InstanceCreationVisitor extends RecursiveAstVisitor { if (_isNotIgnored(argument) && expression is FunctionExpression && - expression.body is BlockFunctionBody) { + _hasNotEmptyBlockBody(expression)) { _expressions.add(argument); } } } + bool _hasNotEmptyBlockBody(FunctionExpression expression) { + final body = expression.body; + if (body is! BlockFunctionBody) { + return false; + } + + return body.block.statements.isNotEmpty; + } + bool _isNotIgnored(Expression argument) => argument is! NamedExpression || (argument.name.label.name != 'builder' && diff --git a/test/analyzers/lint_analyzer/rules/rules_list/prefer_extracting_callbacks/examples/example.dart b/test/analyzers/lint_analyzer/rules/rules_list/prefer_extracting_callbacks/examples/example.dart index c6ed401a6c..3c9dc96df3 100644 --- a/test/analyzers/lint_analyzer/rules/rules_list/prefer_extracting_callbacks/examples/example.dart +++ b/test/analyzers/lint_analyzer/rules/rules_list/prefer_extracting_callbacks/examples/example.dart @@ -65,6 +65,23 @@ class MyAnotherWidget extends StatelessWidget { void _someMethod() {} } +class MyWidgetWithEmptyCallbacks extends StatelessWidget { + @override + Widget build(BuildContext context) { + final widget = AnotherButton( + () {}, + Container(), + ); + + return TextButton( + onPressed: () { + // Just a comment here. + }, + child: Container(), + ); + } +} + class Widget {} class StatelessWidget extends Widget {}