Closed tenhobi closed 1 week ago
Summary: The "Remove the '?'" quick fix incorrectly suggests removing the optional type (?
) from a variable declared in a case
statement when the type is InvalidType
due to missing imports or code generation issues. The fix should be disabled in such cases.
EDIT: I figured out I can put errorFilter there like
await assertNoFix(
errorFilter: (error) =>
error.errorCode != CompileTimeErrorCode.UNDEFINED_CLASS);
Implemented in https://dart-review.googlesource.com/c/sdk/+/394403
I don't know why it would be having a problem finding InvalidType
. The only thing I can think of to suggest is to ensure that you didn't accidentally run dart pub
in your checkout of the sdk. Doing so can sometimes cause problems like this.
But I'd like to suggest that the better fix would be to ensure that StaticWarningCode.UNNECESSARY_NULL_CHECK_PATTERN
isn't reported in this situation. We shouldn't say that it can't be null
when we don't know what type it is. (And the absence of the bogus diagnostic will also prevent the fix from being offered.)
@bwilkerson sorry for maybe misleading error message. The error was about my UnknownType (I just used InvalidType before directly first that is why it's confusing). I also noticed I can filter this error in the test.
But I will check that StaticWarningCode.UNNECESSARY_NULL_CHECK_PATTERN later. It sounds as a better approach. Thanks.
@bwilkerson I made changes so we don't report StaticWarningCode.UNNECESSARY_NULL_CHECK_PATTERN in this case. But I kept tests in remove ? since we probably wanna make sure this will not change. :)
We generally don't have tests for situations that are known to not occur. I haven't looked at the updated CL yet, but if there are tests to ensure that the diagnostic isn't reported when it shouldn't be, then we don't need tests to ensure that the fix isn't offered in those cases.
If we have code like
When my
UnknownType
type isInvalidType
because like in cases when maybe imports are wrong, or generated code is not generated, fix (we have auto fix on save) removes the?
while it is NOT CORRECT and will lead to errors when non null expected below.The fix is there because of this code https://github.com/dart-lang/sdk/blob/main/pkg/analysis_server/lib/src/services/correction/dart/remove_question_mark.dart#L45 which is probably missing some check for
InvalidType
in which case it should not suggest the fix.Edit: The same thing for null assert.