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 S3247 FP: Rule should not trigger when variable is used for data binding on Razor/MVC views #8973

Merged
merged 2 commits into from
Mar 22, 2024

Conversation

gregory-paidis-sonarsource
Copy link
Contributor

Fixes #8735

: parentStatement
.DescendantNodes()
.OfType<CastExpressionSyntax>()
.Where(x => x.Type.WithoutTrivia().IsEquivalentTo(castType.WithoutTrivia())
&& IsCastOnSameSymbol(x))
&& IsCastOnSameSymbol(x)
&& !CSharpFacade.Instance.Syntax.IsInExpressionTree(context.SemanticModel, x)) // see https://github.com/SonarSource/sonar-dotnet/issues/8735#issuecomment-1943419398
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the relevant change, rest is cayc

@@ -0,0 +1,10 @@
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary for asp-for to be properly interpreted by the generator.

If it's not included, we get nothing of value from the razor generator, since it parses asp-for as a random tag that has no specific meaning.
If it is included, we get the thing on the right of asp-for to be bound inside a lambda on the .g.cs file, which is what we are trying to exclude from the rule.

.Single();

compilation.AdditionalSourceFiles.Should().ContainSingle(x => x.EndsWith("Dummy.cshtml"));
compilation.Compilation.SyntaxTrees.Select(x => x.FilePath).Should().Contain(x => x.EndsWith("snippet.0.cs"));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These four tests are testing that:

  • If a snippet name ends with .cshtml, they are added as additional file to the compilation, to be processed by the razor generators.
  • If they either do not have a name or their name ends with .cs, they are not added as additional file, but they are still part of the compilation as a SyntaxTree.

@gregory-paidis-sonarsource
Copy link
Contributor Author

I do not understand the low coverage.
If you look at the Verifier tests I added, I think I am covering all the cases that are pertinent to my change.
Snippet with/without name, and name being .cs or .cshtml.

Copy link

sonarcloud bot commented Mar 22, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

Copy link

sonarcloud bot commented Mar 22, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
73.7% Coverage on New Code (required ≥ 95%)

See analysis details on SonarCloud

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@costin-zaharia-sonarsource
Copy link
Member

I do not understand the low coverage.

I agree that it's strange, I debugged the code and some of the branches reported as uncovered are actually covered.

@costin-zaharia-sonarsource costin-zaharia-sonarsource merged commit 8a64975 into master Mar 22, 2024
26 of 27 checks passed
@costin-zaharia-sonarsource costin-zaharia-sonarsource deleted the greg/fix-S3247-FP branch March 22, 2024 16:01
@martin-strecker-sonarsource
Copy link
Contributor

Peach validation: S3247 no changes

@gregory-paidis-sonarsource
Copy link
Contributor Author

Peach validation: S3247 no changes

@martin-strecker-sonarsource that's fine, right?
It probably means this pattern does not occur on any of the razor/view files of the projects we have on Peach, since the UTs are passing.

@martin-strecker-sonarsource
Copy link
Contributor

Yes. That's fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix S3247 FP: Rule should not trigger when variable is used for data binding on Razor/MVC views
3 participants