Skip to content

Improve internal analyzer regarding AppImplementationFactory #1234

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

Merged
merged 3 commits into from
Apr 7, 2025

Conversation

martinothamar
Copy link
Contributor

Description

  • Also error when trying to use AppImplementationFactory in constructor/field initializer
  • Also error when resolving service through AppImplementationFactory that is not marked with ImplementableByAppsAttribute
  • Misc cleanup and improvements

Did a bunch of manual testing, will add my manual test code as automated tests after #651

Related Issue(s)

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)
  • All tests run green

Documentation

  • User documentation is updated with a separate linked PR in altinn-studio-docs. (if applicable)

@martinothamar martinothamar added ignore-for-release kind/chore backport-ignore This PR is a new feature and should not be cherry-picked onto release branches labels Apr 4, 2025
@martinothamar martinothamar self-assigned this Apr 4, 2025
@martinothamar martinothamar moved this to 👷 In Progress in Team Apps Apr 4, 2025
@martinothamar martinothamar moved this from 👷 In Progress to 🔎 Review in Team Apps Apr 4, 2025
Comment on lines +377 to +381
foreach (var attribute in attributes)
{
if (attribute.AttributeClass?.Name == MarkerAttributeName)
return true;
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where Note

This foreach loop
implicitly filters its target sequence
- consider filtering the sequence explicitly using '.Where(...)'.

Copilot Autofix

AI 25 days ago

To fix the problem, we should replace the foreach loop that filters elements with a foreach loop that iterates over a filtered collection using the Where method. This change will make the code more readable and expressive by clearly indicating the filtering intent.

  • Modify the IsMarkedWithAttribute method to use the Where method for filtering the attributes collection.
  • Specifically, change the foreach loop on line 381 to iterate over attributes.Where(attribute => attribute.AttributeClass?.Name == MarkerAttributeName).
Suggested changeset 1
src/Altinn.App.Internal.Analyzers/AppImplementationInjectionAnalyzer.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Altinn.App.Internal.Analyzers/AppImplementationInjectionAnalyzer.cs b/src/Altinn.App.Internal.Analyzers/AppImplementationInjectionAnalyzer.cs
--- a/src/Altinn.App.Internal.Analyzers/AppImplementationInjectionAnalyzer.cs
+++ b/src/Altinn.App.Internal.Analyzers/AppImplementationInjectionAnalyzer.cs
@@ -380,6 +380,5 @@
         var attributes = symbol.GetAttributes();
-        foreach (var attribute in attributes)
+        foreach (var attribute in attributes.Where(attribute => attribute.AttributeClass?.Name == MarkerAttributeName))
         {
-            if (attribute.AttributeClass?.Name == MarkerAttributeName)
-                return true;
+            return true;
         }
EOF
@@ -380,6 +380,5 @@
var attributes = symbol.GetAttributes();
foreach (var attribute in attributes)
foreach (var attribute in attributes.Where(attribute => attribute.AttributeClass?.Name == MarkerAttributeName))
{
if (attribute.AttributeClass?.Name == MarkerAttributeName)
return true;
return true;
}
Copilot is powered by AI and may make mistakes. Always verify output.
Comment on lines +411 to +422
foreach (var typeArgumentListSyntax in typeArgumentLists)
{
if (typeArgumentListSyntax.Parent is not NameSyntax methodName)
continue;
if (context.SemanticModel.GetSymbolInfo(methodName).Symbol is not IMethodSymbol method)
continue;
if (!factoryMethods.Contains(method.OriginalDefinition, _comparer))
continue;

typeArgumentList = typeArgumentListSyntax;
break;
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where Note

This foreach loop
implicitly filters its target sequence
- consider filtering the sequence explicitly using '.Where(...)'.

Copilot Autofix

AI 25 days ago

To fix the problem, we should replace the foreach loop that filters elements using a continue statement with a foreach loop that iterates over a filtered sequence using the Where method from LINQ. This change will make the code more readable and maintainable by clearly expressing the filtering logic.

  • Modify the foreach loop on line 415 to use the Where method to filter typeArgumentLists before iterating over it.
  • Ensure that the filtering condition typeArgumentListSyntax.Parent is not NameSyntax methodName is moved into the Where method.
Suggested changeset 1
src/Altinn.App.Internal.Analyzers/AppImplementationInjectionAnalyzer.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Altinn.App.Internal.Analyzers/AppImplementationInjectionAnalyzer.cs b/src/Altinn.App.Internal.Analyzers/AppImplementationInjectionAnalyzer.cs
--- a/src/Altinn.App.Internal.Analyzers/AppImplementationInjectionAnalyzer.cs
+++ b/src/Altinn.App.Internal.Analyzers/AppImplementationInjectionAnalyzer.cs
@@ -414,6 +414,5 @@
                 .ToArray();
-            foreach (var typeArgumentListSyntax in typeArgumentLists)
+            foreach (var typeArgumentListSyntax in typeArgumentLists.Where(t => t.Parent is NameSyntax))
             {
-                if (typeArgumentListSyntax.Parent is not NameSyntax methodName)
-                    continue;
+                var methodName = (NameSyntax)typeArgumentListSyntax.Parent;
                 if (context.SemanticModel.GetSymbolInfo(methodName).Symbol is not IMethodSymbol method)
EOF
@@ -414,6 +414,5 @@
.ToArray();
foreach (var typeArgumentListSyntax in typeArgumentLists)
foreach (var typeArgumentListSyntax in typeArgumentLists.Where(t => t.Parent is NameSyntax))
{
if (typeArgumentListSyntax.Parent is not NameSyntax methodName)
continue;
var methodName = (NameSyntax)typeArgumentListSyntax.Parent;
if (context.SemanticModel.GetSymbolInfo(methodName).Symbol is not IMethodSymbol method)
Copilot is powered by AI and may make mistakes. Always verify output.
Copy link

sonarqubecloud bot commented Apr 7, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 65%)

See analysis details on SonarQube Cloud

@martinothamar martinothamar merged commit 63ea12b into main Apr 7, 2025
11 of 12 checks passed
@martinothamar martinothamar deleted the chore/more-internal-analysis branch April 7, 2025 11:19
@github-project-automation github-project-automation bot moved this from 🔎 Review to 🧪 Test in Team Apps Apr 7, 2025
olamathiesenBlueTree pushed a commit to olamathiesenBlueTree/fork-altinn-app-lib-dotnet that referenced this pull request Apr 8, 2025
@martinothamar martinothamar moved this from 🧪 Test to ✅ Done in Team Apps Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-ignore This PR is a new feature and should not be cherry-picked onto release branches ignore-for-release kind/chore
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants