Skip to content

Commit

Permalink
S6966: Add FP repro for #9269 (#9272)
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-strecker-sonarsource authored May 23, 2024
1 parent c707a05 commit 5a3448a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void UseAwaitableMethod_CSharp8() =>
public void UseAwaitableMethod_EF() =>
builder
.WithOptions(ParseOptionsHelper.FromCSharp11)
.AddReferences([CoreMetadataReference.SystemComponentModelTypeConverter])
.AddReferences([CoreMetadataReference.SystemComponentModelTypeConverter, CoreMetadataReference.SystemDataCommon])
.AddReferences(NuGetMetadataReference.MicrosoftEntityFrameworkCore(EntityFrameworkVersion))
.AddReferences(NuGetMetadataReference.MicrosoftEntityFrameworkCoreRelational(EntityFrameworkVersion))
.AddReferences(NuGetMetadataReference.MicrosoftEntityFrameworkCoreSqlServer(EntityFrameworkVersion))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System.IO;
using System.Threading.Tasks;
using System;
using System.Linq;
using System.Collections.Generic;

public class EnitityFramework
{
public async Task Query()
{
// Note to implementers: Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions and RelationalQueryableExtensions might be needed to be added to some sort of whitelist for IQueryables
DbSet<object> dbSet = default;
dbSet.Add(null); // Noncompliant
dbSet.AddRange(null); // Noncompliant
dbSet.Add(null); // Noncompliant FP https://github.com/SonarSource/sonar-dotnet/issues/9269
dbSet.AddRange(null); // Noncompliant FP https://github.com/SonarSource/sonar-dotnet/issues/9269
dbSet.All(x => true); // Noncompliant
dbSet.Any(x => true); // Noncompliant
dbSet.Average(x => 1); // Noncompliant
Expand All @@ -37,4 +38,44 @@ public async Task Query()
dbSet.ToDictionary(x => 0); // Noncompliant
dbSet.ToList(); // Noncompliant
}

public async Task NotIQueryable(IEnumerable<object> enumerable)
{
enumerable.All(x => true); // Compliant not an IQueryable
enumerable.ToArray(); // Compliant
enumerable.ToList(); // Compliant
}

public async Task Context(DbContext dbContext)
{
dbContext.Add(null); // Noncompliant FP https://github.com/SonarSource/sonar-dotnet/issues/9269
dbContext.AddRange(null); // Noncompliant FP https://github.com/SonarSource/sonar-dotnet/issues/9269
dbContext.Dispose(); // Noncompliant
dbContext.Find<object>(); // Noncompliant
dbContext.Find(typeof(object)); // Noncompliant
dbContext.SaveChanges(); // Noncompliant
}

public async Task DatabaseFacade(DatabaseFacade databaseFacade)
{
databaseFacade.BeginTransaction(); // Noncompliant
databaseFacade.CanConnect(); // Noncompliant
databaseFacade.CommitTransaction(); // Noncompliant
databaseFacade.EnsureCreated(); // Noncompliant
databaseFacade.EnsureDeleted(); // Noncompliant
databaseFacade.RollbackTransaction(); // Noncompliant

// Extension methods
bool isEnabled = true;
databaseFacade.BeginTransaction(System.Data.IsolationLevel.Chaos); // Noncompliant
databaseFacade.CloseConnection(); // Noncompliant
databaseFacade.ExecuteSql($"Select * From Table Where IsEnabled = {isEnabled}"); // Noncompliant
databaseFacade.ExecuteSqlInterpolated($"Select * From Table Where IsEnabled = {isEnabled}"); // Noncompliant
databaseFacade.ExecuteSqlRaw("Select * From Table Where IsEnabled = @isEnabled", isEnabled); // Noncompliant
databaseFacade.GetAppliedMigrations(); // Noncompliant
databaseFacade.GetPendingMigrations(); // Noncompliant
databaseFacade.Migrate(); // Noncompliant
databaseFacade.OpenConnection(); // Noncompliant
databaseFacade.UseTransaction(null); // Noncompliant
}
}

0 comments on commit 5a3448a

Please sign in to comment.