dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.28k stars 1.58k forks source link

Lint that helps users realize that `&` is often the best way to create rectangles. #58224

Open jacob314 opened 4 years ago

jacob314 commented 4 years ago

This lint is for users of the Flutter framework to help them discover that the & operator often provides a cleaner way to define rectangles..

Examples

Rect.fromLTWH(offset.dx, offset.dy, size.width, size.height)

Should generate a lint hint that you should write

offset & size

instead.

Additional context It is hard for users to discover that the & operator is often the right way to create a Rect instance. Autocomplete in Dart doesn't suggest operators and for autocomplete to ever help in this case, users would need to know to start with the offset. What we want is some way to suggest a lint or autocomplete style hint whenever a user creates a Rectangle that could be more easily created with & that they should.

Screen Shot 2020-08-26 at 3 50 12 PM
a14n commented 4 years ago

You can also +1 dart-lang/sdk#32174 :)

dnfield commented 4 years ago

This might be unfortunate in the case where the Rect could have been const, but operators won't allow that.

For example:

const Rect rect = Rect.fromLTWH(offset.dx, offset.dy, size.dx, size.dy);

Is valid if offset and size are const, but

const Rect rect = offset & size;

Is not valid.

jacob314 commented 4 years ago

I can't reproduce that case.

const Offset offset = Offset(3.0, 4.0);
const Size size = Size(4.0, 5.0);
const Rect rect = Rect.fromLTWH(offset.dx, offset.dy, size.width, size.height);

Fails with the error: "error: Arguments of a constant creation must be constant expressions. (const_with_non_constant_argument at...."

dnfield commented 4 years ago

Yeah, I edited it to fix - you have to use the .dx and .dy properties, since width and height are getters.

jacob314 commented 4 years ago

Do you mean ._dx and .dy for size? ._dx and ._dy are private on the size object so I'm not clear how those can be used unless your code is in package:flutter

dnfield commented 4 years ago

Ahh ok - I guess this is impossible then. I don't know why I thought I had done it.