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

S2589: Improve message in the case of null propagating operator #8010

Closed
costin-zaharia-sonarsource opened this issue Sep 14, 2023 · 2 comments · Fixed by #8684
Closed

S2589: Improve message in the case of null propagating operator #8010

costin-zaharia-sonarsource opened this issue Sep 14, 2023 · 2 comments · Fixed by #8684
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues.
Milestone

Comments

@costin-zaharia-sonarsource
Copy link
Member

costin-zaharia-sonarsource commented Sep 14, 2023

Description

In some cases, the problem is hard to understand and we should make this more visible for the users.

Repro steps

_componentPage = (_component?.Type == null) ? _parentComponent?.Name : _component?.Name;

or

if (_component?.Type != null)
  return $"components/{_component?.Link}";

image

Expected behavior

For the given examples, the message should make it clear that the null propagating operator is redundant.

Actual behavior

The raised message is "Change this expression which always evaluates to the same result." highlighting the identifier, not the operator, which can be confusing.

@costin-zaharia-sonarsource costin-zaharia-sonarsource added Type: Improvement Area: CFG/SE CFG and SE related issues. Area: C# C# rules related issues. labels Sep 14, 2023
@rjgotten
Copy link

null coalescence operator

The ?. operator is called the null propagating operator.
The null coalescencing operator is ??

@costin-zaharia-sonarsource
Copy link
Member Author

Oh yes, thanks. It seems I was with my mind in another place :)

@costin-zaharia-sonarsource costin-zaharia-sonarsource changed the title S2589: Improve message in the case of null coalescence operator S2589: Improve message in the case of null propagating operator Sep 14, 2023
@Tim-Pohlmann Tim-Pohlmann added this to the 9.20 milestone Feb 5, 2024
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: CFG/SE CFG and SE related issues.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants