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

Fix S1905 FP: Nullability context and array of anonymous types #6438

Closed
paulhickman-a365 opened this issue Nov 24, 2022 · 2 comments · Fixed by #6753
Closed

Fix S1905 FP: Nullability context and array of anonymous types #6438

paulhickman-a365 opened this issue Nov 24, 2022 · 2 comments · Fixed by #6753
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
Milestone

Comments

@paulhickman-a365
Copy link

Description

In a .net 6 project where nullable reference types is enabled, in the example below the casts to string? are necessary, otherwise the nullability of x in the two array elements doesn't match and this produces a CS8206 error

Repro steps

var anon = new[] { new { x = (string?)"foo" }, new { x = (string?)null } };

Expected behavior

No S1905 warning is produced

Actual behavior

S1905 warning is produced

Known workarounds

Don't use anonymous types and create a class with a property x of type string? and declare the array of that type

Related information

  • C# 8 with nullable reference types on
  • VS 2022 19.4.0
  • SonarScanner 8.49.0.57237
  • Operating System Windows Server 2022
@mary-georgiou-sonarsource
Copy link
Contributor

Hello, again @paulhickman-a365, and thanks for opening this issue. Your feedback helps us improve our products. We will look at it as soon as possible. After we confirm the issue, we will add labels and put it in our backlog

@pavel-mikula-sonarsource
Copy link
Contributor

Hi @paulhickman-a365,

Thank you for reporting this, I confirm it as FP.

It also reproduces with #nullable enable

@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. labels Jan 24, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title S1905 erronously suggests removing cast when constructing anonymous type array Fix S1905 FP: Nullability context and array of anonymous types Jan 24, 2023
@github-actions github-actions bot moved this from To do to Review in progress in Best Kanban Feb 13, 2023
@Tim-Pohlmann Tim-Pohlmann moved this from Review in progress to In progress in Best Kanban Feb 14, 2023
@Tim-Pohlmann Tim-Pohlmann moved this from In progress to Review in progress in Best Kanban Feb 15, 2023
@github-actions github-actions bot moved this from Review in progress to In progress in Best Kanban Feb 15, 2023
@github-actions github-actions bot moved this from In progress to Review in progress in Best Kanban Feb 15, 2023
@github-actions github-actions bot moved this from Review in progress to In progress in Best Kanban Feb 16, 2023
@Tim-Pohlmann Tim-Pohlmann moved this from In progress to Review in progress in Best Kanban Feb 27, 2023
@github-actions github-actions bot moved this from Review in progress to In progress in Best Kanban Feb 27, 2023
@github-actions github-actions bot moved this from In progress to Review in progress in Best Kanban Mar 6, 2023
@github-actions github-actions bot moved this from Review in progress to Review approved in Best Kanban Mar 7, 2023
Best Kanban automation moved this from Review approved to Validate Peach Mar 14, 2023
@martin-strecker-sonarsource martin-strecker-sonarsource moved this from Validate Peach to Done in Best Kanban Mar 17, 2023
@martin-strecker-sonarsource martin-strecker-sonarsource added this to the 8.55 milestone Mar 17, 2023
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. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
Best Kanban
  
Done
Development

Successfully merging a pull request may close this issue.

5 participants