From e83987562dbbe78676cf5a1407a8835ad67dedc4 Mon Sep 17 00:00:00 2001 From: vladimir-beloded Date: Fri, 10 Nov 2023 15:32:28 +0200 Subject: [PATCH] Take into account the constructor initializer in the avoid_unused_parameters rule --- .../avoid_unused_parameters_visitor.dart | 7 +++++-- lint_test/avoid_unused_parameters_test.dart | 10 ++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/lints/avoid_unused_parameters/avoid_unused_parameters_visitor.dart b/lib/lints/avoid_unused_parameters/avoid_unused_parameters_visitor.dart index 21760e13..7283cad2 100644 --- a/lib/lints/avoid_unused_parameters/avoid_unused_parameters_visitor.dart +++ b/lib/lints/avoid_unused_parameters/avoid_unused_parameters_visitor.dart @@ -53,6 +53,7 @@ class AvoidUnusedParametersVisitor extends RecursiveAstVisitor { _getUnusedParameters( node.body, parameters.parameters, + initializers: node.initializers, ).whereNot(nameConsistsOfUnderscoresOnly), ); } @@ -104,11 +105,13 @@ class AvoidUnusedParametersVisitor extends RecursiveAstVisitor { Set _getUnusedParameters( AstNode body, - Iterable parameters, - ) { + Iterable parameters, { + NodeList? initializers, + }) { final result = {}; final visitor = _IdentifiersVisitor(); body.visitChildren(visitor); + initializers?.accept(visitor); final allIdentifierElements = visitor.elements; diff --git a/lint_test/avoid_unused_parameters_test.dart b/lint_test/avoid_unused_parameters_test.dart index e54d6499..4d3950f1 100644 --- a/lint_test/avoid_unused_parameters_test.dart +++ b/lint_test/avoid_unused_parameters_test.dart @@ -109,3 +109,13 @@ class TestWidget extends StatelessWidget { return const Placeholder(); } } + +class UsingConstructorParameterInInitializer { + final int _value; + + UsingConstructorParameterInInitializer(int value) : _value = value; + + void printValue() { + print(_value); + } +}