File tree Expand file tree Collapse file tree 3 files changed +33
-1
lines changed
Expand file tree Collapse file tree 3 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 55import 'package:analyzer/analysis_rule/analysis_rule.dart' ;
66import 'package:analyzer/analysis_rule/rule_context.dart' ;
77import 'package:analyzer/analysis_rule/rule_visitor_registry.dart' ;
8+ import 'package:analyzer/dart/analysis/features.dart' ;
89import 'package:analyzer/dart/ast/ast.dart' ;
910import 'package:analyzer/dart/ast/visitor.dart' ;
1011import 'package:analyzer/error/error.dart' ;
@@ -31,6 +32,11 @@ class AvoidFinalParameters extends AnalysisRule {
3132 RuleVisitorRegistry registry,
3233 RuleContext context,
3334 ) {
35+ // This lint isn't relevant with primary constructors enabled
36+ // as `final` is no longer used to indicate a parameter is final,
37+ // but rather as a declaring parameter in a primary constructor.
38+ if (context.isFeatureEnabled (Feature .declaring_constructors)) return ;
39+
3440 var visitor = _Visitor (this );
3541 registry.addConstructorDeclaration (this , visitor);
3642 registry.addFunctionExpression (this , visitor);
Original file line number Diff line number Diff line change @@ -9422,6 +9422,9 @@ LinterLintCode:
94229422 deprecatedDetails : |-
94239423 **Note:** This lint rule was deprecated in Dart 3.11 and
94249424 is set to be removed in a future release of the Dart SDK.
9425+ Also note that the lint is not applied to code that is opted
9426+ in to the `primary-constructors` language feature where using
9427+ `final` for a parameter becomes a compile-time error.
94259428 If you want to ensure parameters aren't reassigned in function bodies,
94269429 consider enabling the
94279430 [`parameter_assignments`](https://dart.dev/lints/parameter_assignments)
Original file line number Diff line number Diff line change 22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5+ import 'package:analyzer/dart/analysis/features.dart' ;
56import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag;
67import 'package:test_reflective_loader/test_reflective_loader.dart' ;
78
@@ -10,11 +11,16 @@ import '../rule_test_support.dart';
1011void main () {
1112 defineReflectiveSuite (() {
1213 defineReflectiveTests (AvoidFinalParametersTest );
14+ defineReflectiveTests (AvoidFinalParametersPrePrimaryConstructorsTest );
1315 });
1416}
1517
1618@reflectiveTest
17- class AvoidFinalParametersTest extends LintRuleTest {
19+ class AvoidFinalParametersPrePrimaryConstructorsTest extends LintRuleTest {
20+ @override
21+ List <String > get experiments =>
22+ super .experiments..remove (Feature .declaring_constructors.enableString);
23+
1824 @override
1925 String get lintRule => LintNames .avoid_final_parameters;
2026
@@ -223,3 +229,20 @@ class B extends A {
223229 );
224230 }
225231}
232+
233+ @reflectiveTest
234+ class AvoidFinalParametersTest extends LintRuleTest {
235+ @override
236+ String get lintRule => LintNames .avoid_final_parameters;
237+
238+ // With primary constructors, this lint is disabled.
239+ // No need to repeat all the tests; one will do.
240+ test_constructorSimple_final () async {
241+ await assertNoDiagnostics (r'''
242+ class C {
243+ // Would be flagged.
244+ C(final int p);
245+ }
246+ ''' );
247+ }
248+ }
You can’t perform that action at this time.
0 commit comments