Roslyn: Suggest to use throw expression with a discard

Created on 1 Mar 2017  路  5Comments  路  Source: dotnet/roslyn

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(..);
Area-IDE Feature Request

Most helpful comment

I don't think assignment should be suggested when there is nothing to assign, just because the code is slightly shorter.

All 5 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MadsTorgersen picture MadsTorgersen  路  3Comments

orthoxerox picture orthoxerox  路  3Comments

joshua-mng picture joshua-mng  路  3Comments

NikChao picture NikChao  路  3Comments

OndrejPetrzilka picture OndrejPetrzilka  路  3Comments