diff --git a/lib/lints/no_magic_number/no_magic_number_rule.dart b/lib/lints/no_magic_number/no_magic_number_rule.dart index 6f282425..be55635b 100644 --- a/lib/lints/no_magic_number/no_magic_number_rule.dart +++ b/lib/lints/no_magic_number/no_magic_number_rule.dart @@ -70,7 +70,8 @@ class NoMagicNumberRule extends SolidLintRule { .where(_isNotInDateTime) .where(_isNotInsideIndexExpression) .where(_isNotInsideEnumConstantArguments) - .where(_isNotDefaultValue); + .where(_isNotDefaultValue) + .where(_isNotInConstructorInitializer); for (final magicNumber in magicNumbers) { reporter.reportErrorForNode(code, magicNumber); @@ -127,4 +128,8 @@ class NoMagicNumberRule extends SolidLintRule { bool _isNotDefaultValue(Literal literal) { return literal.thisOrAncestorOfType() == null; } + + bool _isNotInConstructorInitializer(Literal literal) { + return literal.thisOrAncestorOfType() == null; + } } diff --git a/lint_test/no_magic_number_test.dart b/lint_test/no_magic_number_test.dart index 239564a5..454ca326 100644 --- a/lint_test/no_magic_number_test.dart +++ b/lint_test/no_magic_number_test.dart @@ -79,3 +79,10 @@ class DefaultValues { void topLevelFunctionWithDefaultParam({int value = 6}) { ({int value = 7}) {}; } + +// Allowed for numbers in constructor initializer. +class ConstructorInitializer { + final int value; + + ConstructorInitializer() : value = 10; +}