Skip to content

Lint request - avoid_cast_from_null #58822

@natebosch

Description

@natebosch

In mockito the any and anyNamed utilities allow flexible targeting for stub behavior and required being able to be passed to any argument position. Before null safety this was accomplished by giving them the value of null which could be passed to any argument type. With null safety, non-nullable arguments can't use this technique. With codegen, the Mock* subclass widens all argument types to be nullable. This requires that the type of an expression in a when argument needs to be typed as the Mock* class instead of the mocked type so at the position of the call passing any the argument is statically nullable.

This isn't always obvious, and a potential mistake during a migration is to try to cast the any to another type like when(someMock.someCall(any as int)). This cast is guaranteed to fail when running with sound null safety.

We should consider a lint which warns when an expression with type Null is cast to any non-nullable type, since that cast will always fail when running when sound null safety.

Metadata

Metadata

Assignees

Labels

devexp-linterIssues with the analyzer's support for the linter packagelegacy-area-analyzerUse area-devexp instead.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions