Open shilangyu opened 3 months ago
Summary: The unnecessary_set_literal
lint incorrectly fails to detect unnecessary set literals when passed as named arguments or assigned to variables. This leads to potential errors, especially in common Flutter scenarios like onPressed
callbacks.
The problem stems from this line: https://github.com/dart-lang/sdk/blob/2cf3222e4f1c00c36e1b072c1de8db31dad01de7/pkg/analyzer/lib/src/error/best_practices_verifier.dart#L1288
Changing it to final parameterType = node.staticType;
fixes the issue. But actually it seems to be a bug that the current version does not work with named arguments. For those node.staticParameterElement
is null, which doesn't seem like it should be the happening.
Consider the following snippet:
The
unnecessary_set_literal
lint only works when the function expression is passed to a function as a position argument. I see no reason why the other two examples should not be linted as well.To underline the importance of this lint working with named arguments consider the very common
onPressed
named argument in flutter widgets. This is a common place where this accidental set literal problem can arise. For example when adding anonPressed
that will just mutate some state:Tested on Dart SDK 3.6.0-88.0.dev