Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Rule Idea: Null forgiving operator and conditional access should not be gratuitous/detrimental #7320

Closed
gregory-paidis-sonarsource opened this issue Jun 1, 2023 · 7 comments
Labels
Area: C# C# rules related issues. Area: VB.NET VB.NET rules related issues. Type: Rule Idea Idea for a rule that has NOT been specified.

Comments

@gregory-paidis-sonarsource
Copy link
Contributor

There are two mechanisms to either check for null or ignore a null check:

There are some cases that one of them might be either redundant, or actually making the program crash every time:

string neverNull = "not null";
_ = neverNull?.ToString(); // Noncompliant: conditional access is redundant, always valid
_ = neverNull!.ToString(); // Noncompliant: null-forgiving is redundant, no reason to check

string alwaysNull = null;
_ = alwaysNull?.ToString(); // always evaluates to null. Most likely we should not raise here, as it prevents a NRE 
_ = alwaysNull!.ToString(); // Noncompliant: always NRE, operator is detrimental

It might be two rules as well, one for null-forgiving ! and one for conditional access ?.

@gregory-paidis-sonarsource gregory-paidis-sonarsource added Area: VB.NET VB.NET rules related issues. Area: C# C# rules related issues. Type: Rule Idea Idea for a rule that has NOT been specified. labels Jun 1, 2023
@pavel-mikula-sonarsource
Copy link
Contributor

S2583 and S2589 might take care of the ? part after MMF-2563

@luizfbicalho
Copy link

Thanks for the issue

I think that one problem when implementing nullable reference types, is that after a ?. in a non nullable code

for example this code

public string? GetString()
{
    return DateTime.Now.Seconds>10?"Bigger":null;
}

then After that your code in the example

string? neverNull = GetString();
_ = neverNull?.ToString(); // This is OK
_ = neverNull!.ToString(); // This shoudn't be ok, I think that should validate and not use this

But the problem is that after each code, the nullable reference type misses the nullable information

image image

in my example, the fact that I used an ?. n the line

var y = x?.ToString();

then after that x becomes nullable

and the same happens with the exclamation

image image

so this problem is bad because the null reference type information flows through the code after one misuse

@luizfbicalho
Copy link

Same issue is thacked in dotnet Roslyn
dotnet/roslyn#68267

@cristian-ambrosini-sonarsource cristian-ambrosini-sonarsource changed the title Null forgiving operator and conditional access should not be gratuitous/detrimental New Rule Idea: Null forgiving operator and conditional access should not be gratuitous/detrimental Nov 17, 2023
@luizfbicalho
Copy link

Any information on this rule?

@luizfbicalho
Copy link

I created a roslyn analyzer to implement this, but its not working as expected, but if anyone is interested in add some thoughts https://github.com/luizfbicalho/NullableAnalyzer

@mary-georgiou-sonarsource
Copy link
Contributor

mary-georgiou-sonarsource commented Oct 31, 2024

After considering this rule we decided to specify the following:
Conditional access should not be gratuitous.

string s = Test(true);
_ = s?.ToString();  // Noncompliant, you should remove ? and if you want to suppress a warning use !

string Test(bool condition) => condition ? "Hello": null;

and add a new rule idea
Don't use ! if you are in non-nullable context

@mary-georgiou-sonarsource
Copy link
Contributor

One more update:
We realized that the following rule:

After considering this rule we decided to specify the following: Conditional access should not be gratuitous.

string s = Test(true);
_ = s?.ToString();  // Noncompliant, you should remove ? and if you want to suppress a warning use !

string Test(bool condition) => condition ? "Hello": null;

I overlapping with S2583/S2589 so we decided to not proceed with its implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: VB.NET VB.NET rules related issues. Type: Rule Idea Idea for a rule that has NOT been specified.
Projects
None yet
Development

No branches or pull requests

4 participants