reduce false positive for sized_box_for_whitespace (#2067)
diff --git a/lib/src/rules/sized_box_for_whitespace.dart b/lib/src/rules/sized_box_for_whitespace.dart
index 50c8644..7a16050 100644
--- a/lib/src/rules/sized_box_for_whitespace.dart
+++ b/lib/src/rules/sized_box_for_whitespace.dart
@@ -12,7 +12,7 @@
const _details = r'''Use SizedBox to add whitespace to a layout.
-A `Container` is a heavier Widget than a `SizedBox`, and as bonus, `SizedBox`
+A `Container` is a heavier Widget than a `SizedBox`, and as bonus, `SizedBox`
has a `const` constructor.
**BAD:**
@@ -76,24 +76,37 @@
final visitor = _WidthOrHeightArgumentVisitor();
node.visitChildren(visitor);
- if (visitor.seenWidthOrHeight && !visitor.seenOtherParams) {
+ if (visitor.seenIncompatibleParams) {
+ return;
+ }
+ if (visitor.seenChild && (visitor.seenWidth || visitor.seenHeight) ||
+ visitor.seenWidth && visitor.seenHeight) {
rule.reportLint(node.constructorName);
}
}
}
class _WidthOrHeightArgumentVisitor extends SimpleAstVisitor<void> {
- var seenWidthOrHeight = false;
- var seenOtherParams = false;
+ var seenWidth = false;
+ var seenHeight = false;
+ var seenChild = false;
+ var seenIncompatibleParams = false;
@override
void visitArgumentList(ArgumentList node) {
- for (final arg in node.arguments) {
- if (arg is NamedExpression &&
- (arg.name.label.name == 'width' || arg.name.label.name == 'height')) {
- seenWidthOrHeight = true;
+ for (final name in node.arguments
+ .cast<NamedExpression>()
+ .map((arg) => arg.name.label.name)) {
+ if (name == 'width') {
+ seenWidth = true;
+ } else if (name == 'height') {
+ seenHeight = true;
+ } else if (name == 'child') {
+ seenChild = true;
+ } else if (name == 'key') {
+ // key doesn't matter (both SiezdBox and Container have it)
} else {
- seenOtherParams = true;
+ seenIncompatibleParams = true;
}
}
}
diff --git a/test/rules/sized_box_for_whitespace.dart b/test/rules/sized_box_for_whitespace.dart
index 32f0ef9..2c879a5 100644
--- a/test/rules/sized_box_for_whitespace.dart
+++ b/test/rules/sized_box_for_whitespace.dart
@@ -13,21 +13,21 @@
}
Widget containerWithChildAndWidth() {
- return Container( // OK
+ return Container( // LINT
width: 10,
child: Row(),
);
}
Widget containerWithChildAndHeight() {
- return Container( // OK
+ return Container( // LINT
height: 10,
child: Column(),
);
}
Widget containerWithChildWidthAndHeight() {
- return Container( // OK
+ return Container( // LINT
width: 10,
height: 10,
child: Row(),
@@ -40,13 +40,13 @@
}
Widget emptyContainerWithWidth() {
- return Container( // LINT
+ return Container( // OK
width: 10,
);
}
Widget emptyContainerWithHeight() {
- return Container( // LINT
+ return Container( // OK
height:10,
);
}
@@ -57,3 +57,11 @@
height: 10,
);
}
+
+Widget emptyContainerWithKeyAndWidthAndHeight() {
+ return Container( // LINT
+ key: null,
+ width: 10,
+ height: 10,
+ );
+}