Currently there is a code action that suggests to use throw expression only if the target is being assigned
if ( p == null ) throw new ArgumentNullException(..); this.p = p;
->
this.p = p ?? throw new ArgumentNullException(..);
This could be done even if it's not being assigned,
if ( p == null ) throw new ArgumentNullException(..);
->
_ = p ?? throw new ArgumentNullException(..);
I don't think assignment should be suggested when there is nothing to assign, just because the code is slightly shorter.
because the code is slightly shorter.
Exactly. Note that not everybody likes even the existing suggestion; you should opt-in anyways, so I don't see why we should not do this for everybody.
PS: Only if we could use ??
as a statement .. .
This doesn't seem like the sort of suggestion we would build into the product, but feel free to write as an extension analyzer/fixer.
@kuhlenh @DustinCampbell @CyrusNajmabadi any disagreement here?
I agree on not doing this. Generating an assign just so you can use a ?? seems not great.
Most helpful comment
I don't think assignment should be suggested when there is nothing to assign, just because the code is slightly shorter.