WiseTechGlobal / WTG.Analyzers

Analyzers from WiseTech Global to enforce our styles, behaviours, and prevent common mistakes.
Other
16 stars 3 forks source link

Analyzer to detect potential foreach and await null dereferences #31

Closed yaakov-h closed 4 years ago

yaakov-h commented 6 years ago

Warning:

await foo?.Bar()

OK:

await (foo?.Bar() ?? something)

Warning:

foreach (var i in foo?.Bar())

OK:

foreach (var i in foo?.Bar() ?? something)
brian-reichle commented 6 years ago

The second example seems like invalid syntax since the await has a higher precedence than the null-coalescing operator.

yaakov-h commented 6 years ago

Updated

brian-reichle commented 6 years ago

Should probably also be aware of as in the same contexts

foreach (var item in (foo as IEnumerable))

or

await (foo as Task)
yaakov-h commented 6 years ago

This might be easier with C# 8 (I assume Roslyn 3.0?), because the compiler will track the nullability state of the expression... hopefully that will be exposed.

brian-reichle commented 4 years ago

There doesn't seem to be anything to implement here beyond what nullable reference types already provides.

https://sharplab.io/#v2:CYLg1APgxAdgrgGwQQwEYIKYAIMzZgWACgABABixIEYAWAbmPMqoDoAZASxgEcHSLqAVj6MAzJQBMWAMLEA3sSxLK4kgA5KANiwBZABQB7VACsMAYwAuWZACcA5gEpFyhUWXvKATiwAxAwb0HPg9lEm89WztrAGctIOcQsKw9PwCHLAB+DK0WaQMAWwAHTAsMYBJNeLcQrASPADMDGwxkMwALZIA3WywOUvzemCwAIVtAp2qQ1xqsAF869wXlRubWjr1um17+wet7GOZRAB4jU0sAPgmZ6Zr5yY8lpRWW9q6evowBrhGx9KysACi8HyGBs+AwLABRQsAE8TiZzBZzuNHlgbiE7u5Mco6tRtBVsqlAlgALznLDwJDBUJUfFUY6nRHnbKjGzEskUxAIPizIA==