-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix false positive use_decorated_box
and use_colored_box
#3271
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,7 @@ | |
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
|
||
class Color { | ||
Color(int value); | ||
} | ||
class Color {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can't do so in this PR because the change has to be made to the analyzer package, but this class really ought to be part of the mock SDK we use for testing. When we do that we'll make the class match the published version of Once it's been added, then this test will need to be converted to using the definition from the mock SDK, and that will require restoring all of the arguments. It would ease that future conversion if you could revert the change to the definition of |
||
|
||
Widget containerWithoutArguments() { | ||
return Container(); // OK | ||
|
@@ -23,7 +21,7 @@ Widget containerWithKey() { | |
|
||
Widget containerWithColor() { | ||
return Container( // LINT | ||
color: Color(0xffffffff), | ||
color: Color(), | ||
); | ||
} | ||
|
||
|
@@ -43,21 +41,21 @@ Widget containerWithKeyAndChild() { | |
Widget containerWithKeyAndColor() { | ||
return Container( // LINT | ||
key: Key('abc'), | ||
color: Color(0xffffffff), | ||
color: Color(), | ||
); | ||
} | ||
|
||
Widget containerWithColorAndChild() { | ||
return Container( // LINT | ||
color: Color(0xffffffff), | ||
color: Color(), | ||
child: SizedBox(), | ||
); | ||
} | ||
|
||
Widget containerWithKeyAndColorAndChild() { | ||
return Container( // LINT | ||
key: Key('abc'), | ||
color: Color(0xffffffff), | ||
color: Color(), | ||
child: SizedBox(), | ||
); | ||
} | ||
|
@@ -70,15 +68,49 @@ Widget containerWithAnotherArgument() { | |
|
||
Widget containerWithColorAndAdditionalArgument() { | ||
return Container( // OK | ||
color: Color(0xffffffff), | ||
color: Color(), | ||
width: 20, | ||
); | ||
} | ||
|
||
Widget containerWithColorAndAdditionalArgumentAndChild() { | ||
return Container( // OK | ||
color: Color(0xffffffff), | ||
color: Color(), | ||
width: 20, | ||
child: SizedBox(), | ||
); | ||
} | ||
|
||
Widget containerWithNullColor() { | ||
return Container( // OK | ||
color: null, | ||
child: SizedBox(), | ||
); | ||
} | ||
|
||
const Color? _nullableColor = null; | ||
|
||
Widget containerWithNullableColor() { | ||
return Container( // OK | ||
color: _nullableColor, | ||
child: SizedBox(), | ||
); | ||
} | ||
|
||
final _nonNullColor = Color(); | ||
|
||
Widget containerWithNonNullColor() { | ||
return Container( // LINT | ||
color: _nonNullColor, | ||
child: SizedBox(), | ||
); | ||
} | ||
|
||
Color? _getColor() => null; | ||
|
||
Widget nullableReturnValue() { | ||
return Container( // OK | ||
color: _getColor(), | ||
child: SizedBox(), | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ Widget containerWithKey() { | |
|
||
Widget containerWithDecoration() { | ||
return Container( // LINT | ||
decoration: BoxDecoration(), | ||
decoration: Decoration(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that these changes (from |
||
); | ||
} | ||
|
||
|
@@ -40,21 +40,21 @@ Widget containerWithKeyAndChild() { | |
Widget containerWithKeyAndDecoration() { | ||
return Container( // LINT | ||
key: Key('abc'), | ||
decoration: BoxDecoration(), | ||
decoration: Decoration(), | ||
); | ||
} | ||
|
||
Widget containerWithDecorationAndChild() { | ||
return Container( // LINT | ||
decoration: BoxDecoration(), | ||
decoration: Decoration(), | ||
child: SizedBox(), | ||
); | ||
} | ||
|
||
Widget containerWithKeyAndDecorationAndChild() { | ||
return Container( // LINT | ||
key: Key('abc'), | ||
decoration: BoxDecoration(), | ||
decoration: Decoration(), | ||
child: SizedBox(), | ||
); | ||
} | ||
|
@@ -67,15 +67,49 @@ Widget containerWithAnotherArgument() { | |
|
||
Widget containerWithDecorationAndAdditionalArgument() { | ||
return Container( // OK | ||
decoration: BoxDecoration(), | ||
decoration: Decoration(), | ||
width: 20, | ||
); | ||
} | ||
|
||
Widget containerWithDecorationAndAdditionalArgumentAndChild() { | ||
return Container( // OK | ||
decoration: BoxDecoration(), | ||
decoration: Decoration(), | ||
width: 20, | ||
child: SizedBox(), | ||
); | ||
} | ||
|
||
Widget containerWithNullDecoration() { | ||
return Container( // OK | ||
decoration: null, | ||
child: SizedBox(), | ||
); | ||
} | ||
|
||
const Decoration? _nullableDecoration = null; | ||
|
||
Widget containerWithNullableDecoration() { | ||
return Container( // OK | ||
decoration: _nullableDecoration, | ||
child: SizedBox(), | ||
); | ||
} | ||
|
||
final _nonNullDecoration = Decoration(); | ||
|
||
Widget containerWithNonNullDecoration() { | ||
return Container( // LINT | ||
decoration: _nonNullDecoration, | ||
child: SizedBox(), | ||
); | ||
} | ||
|
||
Decoration? _getDecoration() => null; | ||
|
||
Widget nullableReturnValue() { | ||
return Container( // OK | ||
decoration: _getDecoration(), | ||
child: SizedBox(), | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking for nullability is good, but we're missing the more important test to ensure that the type of the expression is assignable to
BoxDecoration
.If you wouldn't mind fixing that while you're at it, then also add a test to ensure that we don't lint when the
decoration
argument is some other kind of decoration (maybe aShapeDecoration
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A
Container
accepts aDecoration?
, which looks like a deliberate design decision to me. This also doesn't throw an error:If a Container is supposed to only accept a
BoxDecoration
, why not just allow the constructor to accept aBoxDecoration
instead of aDecoration
?