Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,38 @@ internal class {|MA0053:SampleException|}
return test.RunAsync();
}

[Fact]
public Task VirtualMember_EditorConfig_DocumentedConfigurationName()
{
var test = CreateTest();
test.TestState.SetConfiguration("MA0053.class_with_virtual_member_should_be_sealed", "true");
test.TestCode = """
internal class {|MA0053:SampleException|}
{
protected virtual void A() => throw null;
}
""";

return test.RunAsync();
}

[Fact]
public Task VirtualMember_EditorConfig_DocumentedConfigurationNameOverridesLegacyOne()
{
var test = CreateTest();
test.TestState.SetConfiguration(
("MA0053.class_with_virtual_member_shoud_be_sealed", "true"),
("MA0053.class_with_virtual_member_should_be_sealed", "false"));
test.TestCode = """
internal class SampleException
{
protected virtual void A() => throw null;
}
""";

return test.RunAsync();
}

[Fact]
public Task ComImport()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4170,6 +4170,52 @@ private void A(SqliteConnection connection)
return test.RunAsync();
}

[Fact]
[Trait("Issue", "https://github.com/meziantou/Meziantou.Analyzer/issues/1121")]
public Task PrivateNonAsync_SqliteConnection_Close_MA0045OptionDisabled_Diagnostic()
{
var test = new AnalyzerTest();
test.TestState.SetConfiguration("MA0045.enable_sqlite_special_cases", "false");
test.ReferenceAssemblies = test.ReferenceAssemblies.AddSqlite();
test.TestCode = """
using Microsoft.Data.Sqlite;

class Test
{
private void A(SqliteConnection connection)
{
{|MA0045:connection.Close()|};
}
}
""";

return test.RunAsync();
}

[Fact]
[Trait("Issue", "https://github.com/meziantou/Meziantou.Analyzer/issues/1121")]
public Task PrivateNonAsync_SqliteConnection_Close_MA0045OptionEnabledOverridesMA0042Option_NoDiagnostic()
{
var test = new AnalyzerTest();
test.TestState.SetConfiguration(
("MA0042.enable_sqlite_special_cases", "false"),
("MA0045.enable_sqlite_special_cases", "true"));
test.ReferenceAssemblies = test.ReferenceAssemblies.AddSqlite();
test.TestCode = """
using Microsoft.Data.Sqlite;

class Test
{
private void A(SqliteConnection connection)
{
connection.Close();
}
}
""";

return test.RunAsync();
}

[Fact]
[Trait("Issue", "https://github.com/meziantou/Meziantou.Analyzer/issues/1121")]
public Task PrivateNonAsync_SqliteCommand_Prepare_OptionDisabled_Diagnostic()
Expand Down Expand Up @@ -4298,6 +4344,72 @@ class MyDbTransaction : DbTransaction
return test.RunAsync();
}

[Fact]
[Trait("Issue", "https://github.com/meziantou/Meziantou.Analyzer/issues/1134")]
public Task PrivateNonAsync_UsingFactoryMethod_DbTransaction_NoDisposeAsyncOverride_MA0045OptionDisabled_Diagnostic()
{
var test = new AnalyzerTest();
test.TestState.SetConfiguration("MA0045.enable_db_special_cases", "false");
test.TestCode = """
using System.Data;
using System.Data.Common;

class Test
{
private void A()
{
{|MA0045:using var transaction = CreateTransaction();|}
}

private MyDbTransaction CreateTransaction() => throw null;
}

class MyDbTransaction : DbTransaction
{
protected override DbConnection DbConnection => throw null;
public override IsolationLevel IsolationLevel => throw null;
public override void Commit() => throw null;
public override void Rollback() => throw null;
}
""";

return test.RunAsync();
}

[Fact]
[Trait("Issue", "https://github.com/meziantou/Meziantou.Analyzer/issues/1134")]
public Task PrivateNonAsync_UsingFactoryMethod_DbTransaction_NoDisposeAsyncOverride_MA0045OptionEnabledOverridesMA0042Option_NoDiagnostic()
{
var test = new AnalyzerTest();
test.TestState.SetConfiguration(
("MA0042.enable_db_special_cases", "false"),
("MA0045.enable_db_special_cases", "true"));
test.TestCode = """
using System.Data;
using System.Data.Common;

class Test
{
private void A()
{
using var transaction = CreateTransaction();
}

private MyDbTransaction CreateTransaction() => throw null;
}

class MyDbTransaction : DbTransaction
{
protected override DbConnection DbConnection => throw null;
public override IsolationLevel IsolationLevel => throw null;
public override void Commit() => throw null;
public override void Rollback() => throw null;
}
""";

return test.RunAsync();
}

[Fact]
public Task ExcludeFromBlockingCallAnalysisAttribute_DocumentationIdMethod_NonAsyncContext()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,93 @@ class Test
return test.RunAsync();
}

[Fact]
public Task StringConcat_ToString_Int32()
{
var test = CreateTest();
test.TestCode = """
class Test
{
void ToString() { _ = "abc" + -1; }
}
""";

return test.RunAsync();
}

[Fact]
public Task StringConcat_ToString_Int32_ConfigNotExcludeToString()
{
var test = CreateTest();
test.TestState.SetConfiguration("MA0075.exclude_tostring_methods", "false");
test.TestCode = """
class Test
{
void ToString() { _ = "abc" + {|MA0075:-1|}; }
}
""";

return test.RunAsync();
}

[Fact]
public Task StringConcat_NullableInt32()
{
var test = CreateTest();
test.TestCode = """
class Test
{
void A() { _ = "abc" + {|MA0075:(int?)-1|}; }
}
""";

return test.RunAsync();
}

[Fact]
public Task StringConcat_NullableInt32_ConfigNotConsiderNullableTypes()
{
var test = CreateTest();
test.TestState.SetConfiguration("MA0075.consider_nullable_types", "false");
test.TestCode = """
class Test
{
void A() { _ = "abc" + (int?)-1; }
}
""";

return test.RunAsync();
}

[Fact]
public Task StringInterpolation_NullableInt32()
{
var test = CreateTest();
test.TestCode = """
class Test
{
void A() { _ = $"{|MA0076:{(int?)-1}|}"; }
}
""";

return test.RunAsync();
}

[Fact]
public Task StringInterpolation_NullableInt32_ConfigNotConsiderNullableTypes()
{
var test = CreateTest();
test.TestState.SetConfiguration("MA0076.consider_nullable_types", "false");
test.TestCode = """
class Test
{
void A() { _ = $"{(int?)-1}"; }
}
""";

return test.RunAsync();
}

[Fact]
public Task ObjectToString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ public Task NestedType_DoNotReportDiagnostic()
return test.RunAsync();
}

[Fact]
public Task InternalType_DoNotReportDiagnostic()
{
var test = CreateTest();
test.TestCode = "internal class Action { }";

return test.RunAsync();
}

[Fact]
public Task InternalType_ConsiderNonPublicSymbols_ReportDiagnostic()
{
var test = CreateTest();
test.TestState.SetConfiguration("MA0104.only_consider_public_symbols", "false");
test.TestCode = "internal class {|MA0104:Action|} { }";

return test.RunAsync();
}

[Fact]
public Task Regex_DoNotReportDiagnostic()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,41 @@ public Task SeriLog_Log_Information_Exception()
return test.RunAsync();
}

[Fact]
public Task SeriLog_Log_Information_StringConcat()
{
var test = CreateTest();
test.TestCode = """
using Serilog;

var a = "test";
Log.Information("{Prop} " + a, {|MA0139:(int?)1|});
""";
test.TestState.AdditionalFiles.Add(("LoggerParameterTypes.txt", """
Prop;System.Int32
"""));

return test.RunAsync();
}

[Fact]
public Task SeriLog_Log_Information_StringConcat_NonConstantDisabled()
{
var test = CreateTest();
test.TestState.SetConfiguration("MA0139.allow_non_constant_formats", "false");
test.TestCode = """
using Serilog;

var a = "test";
Log.Information("{Prop} " + a, (int?)1);
""";
test.TestState.AdditionalFiles.Add(("LoggerParameterTypes.txt", """
Prop;System.Int32
"""));

return test.RunAsync();
}

[Fact]
public Task SeriLog_Log_Information_Params()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,46 @@ void MyMethod(int a, long b, short c) { }
return test.RunAsync();
}

[Fact]
public Task Int32_ExcludedMethodDeclarationId_ShouldNotReportDiagnostic()
{
var test = CreateTest();
test.TestState.SetConfiguration(("MA0003.expression_kinds", "numeric"), ("MA0003.excluded_methods", "M:Other.Method|M:TypeName.MyMethod(System.Int32,System.Int64,System.Int16)"));
test.TestCode = """
class TypeName
{
public void Test()
{
MyMethod(1, 1L, 3);
}

void MyMethod(int a, long b, short c) { }
}
""";

return test.RunAsync();
}

[Fact]
public Task Int32_ExcludedMethodDeclarationIdOfAnotherMethod_ShouldReportDiagnostic()
{
var test = CreateTest();
test.TestState.SetConfiguration(("MA0003.expression_kinds", "numeric"), ("MA0003.excluded_methods", "M:TypeName.MyMethod(System.Int32)"));
test.TestCode = """
class TypeName
{
public void Test()
{
MyMethod({|MA0003:1|}, {|MA0003:1L|}, {|MA0003:3|});
}

void MyMethod(int a, long b, short c) { }
}
""";

return test.RunAsync();
}

[Fact]
public Task Int32_ExcludedMethodWithEmptyRegex_ShouldReportDiagnostic()
{
Expand Down
Loading