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

.NET 8 and C# 12: add repros and UTs for rules starting with R in SonarWay #8093

Merged
merged 16 commits into from
Oct 2, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
S4428: Add UTs for type aliases
  • Loading branch information
antonioaversa committed Sep 26, 2023
commit 5f80b734f38b1f8d2c1f810093c0c1f71ef25283
Original file line number Diff line number Diff line change
@@ -1,87 +1,106 @@
using System;
using System.ComponentModel.Composition;

namespace Tests.Diagnostics
[Export(typeof(object))]
[PartCreationPolicy(CreationPolicy.Any)] // Compliant, Export is present
class Program1
{
[Export(typeof(object))]
[PartCreationPolicy(CreationPolicy.Any)] // Compliant, Export is present
class Program1
{
}

[InheritedExport(typeof(object))]
[PartCreationPolicy(CreationPolicy.Any)] // Compliant, InheritedExport is present
class Program2_Base
{
}

[PartCreationPolicy(CreationPolicy.Any)] // Compliant, InheritedExport is present in base
class Program2 : Program2_Base
{
}
}

[InheritedExport(typeof(object))]
[PartCreationPolicy(CreationPolicy.Any)] // Compliant, InheritedExport is present
class Program2_Base
{
}

[PartCreationPolicy(CreationPolicy.Any)] // Compliant, InheritedExport is present in base
class Program2 : Program2_Base
{
}

[PartCreationPolicy(CreationPolicy.Any)] // Noncompliant {{Add the 'ExportAttribute' or remove 'PartCreationPolicyAttribute' to/from this type definition.}}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
class Program3
{
}
class Program3
{
}

[PartCreationPolicy(CreationPolicy.Any)] // Noncompliant, Export is not inherited
class Program4 : Program1
{
}

[PartCreationPolicy(CreationPolicy.Any)] // Noncompliant, Export is not inherited
class Program4 : Program1
{
}
class Program5
{
[PartCreationPolicy(CreationPolicy.Any)] // Error [CS0592] - Compliant, attribute cannot be used on methods, don't raise
public void Method() { }

class Program5
{
[PartCreationPolicy(CreationPolicy.Any)] // Error [CS0592] - Compliant, attribute cannot be used on methods, don't raise
public void Method() { }
[PartCreationPolicy(CreationPolicy.Any)] // Error [CS0592] - Compliant, attribute cannot be used on fields, don't raise
public int Field;

[PartCreationPolicy(CreationPolicy.Any)] // Error [CS0592] - Compliant, attribute cannot be used on fields, don't raise
public int Field;
[PartCreationPolicy(CreationPolicy.Any)] // Error [CS0592] - Compliant, attribute cannot be used on properties, don't raise
public int Property { get; set; }
}

[PartCreationPolicy(CreationPolicy.Any)] // Error [CS0592] - Compliant, attribute cannot be used on properties, don't raise
public int Property { get; set; }
}
[MyExport]
[PartCreationPolicy(CreationPolicy.Any)] // Compliant, custom Export is present
class Program6 { }

[MyExport]
[PartCreationPolicy(CreationPolicy.Any)] // Compliant, custom Export is present
class Program6 { }
[MyInheritedExport(typeof(object))]
[PartCreationPolicy(CreationPolicy.Any)] // Compliant, MyInheritedExport is present
class Program7_Base { }

[MyInheritedExport(typeof(object))]
[PartCreationPolicy(CreationPolicy.Any)] // Compliant, MyInheritedExport is present
class Program7_Base { }
[PartCreationPolicy(CreationPolicy.Any)] // Compliant, MyInheritedExport is present in base
class Program7 : Program2_Base { }

[PartCreationPolicy(CreationPolicy.Any)] // Compliant, MyInheritedExport is present in base
class Program7 : Program2_Base { }
[Foo]
[PartCreationPolicy(CreationPolicy.Any)] // Noncompliant {{Add the 'ExportAttribute' or remove 'PartCreationPolicyAttribute' to/from this type definition.}}
class Program8 { }

[Foo]
[PartCreationPolicy(CreationPolicy.Any)] // Noncompliant {{Add the 'ExportAttribute' or remove 'PartCreationPolicyAttribute' to/from this type definition.}}
class Program8 { }
[PartCreationPolicy(CreationPolicy.NonShared)] // Compliant, InheritedExport is present on interface
class Program9 : IMyInheritedExportInterface { }

[PartCreationPolicy(CreationPolicy.NonShared)] // Compliant, InheritedExport is present on interface
class Program9 : IMyInheritedExportInterface { }
[PartCreationPolicy(CreationPolicy.Shared)] // Compliant, MyInheritedExport is present on interface MyInheritedExportInterface
class Program11 : IFoo, IBar, IMyInheritedExportInterface, IQix { }

[PartCreationPolicy(CreationPolicy.Shared)] // Compliant, MyInheritedExport is present on interface MyInheritedExportInterface
class Program11 : IFoo, IBar, IMyInheritedExportInterface, IQix { }
[PartCreationPolicy(CreationPolicy.NonShared)] // Noncompliant
class Program12 : IFoo, IBar, IQix { }

[PartCreationPolicy(CreationPolicy.NonShared)] // Noncompliant
class Program12 : IFoo, IBar, IQix { }
class MyExportAttribute : ExportAttribute { }

class MyExportAttribute : ExportAttribute { }
class MyInheritedExportAttribute : InheritedExportAttribute
{
public MyInheritedExportAttribute(System.Type type) { }
}

class MyInheritedExportAttribute : InheritedExportAttribute
{
public MyInheritedExportAttribute(System.Type type) { }
}
class FooAttribute : Attribute { }

[InheritedExport(typeof(object))]
interface IMyInheritedExportInterface { }

interface IFoo { }
interface IBar { }
interface IQix { }

[PartCreationPolicy(CreationPolicy.Any)] // Error [CS0116] - Compliant, illegal use, don't raise

namespace Aliases
{
using AliasForPartCreationPolicy = System.ComponentModel.Composition.PartCreationPolicyAttribute;
using AliasForExport = System.ComponentModel.Composition.ExportAttribute;
using AliasForInheritedExport = System.ComponentModel.Composition.InheritedExportAttribute;

class FooAttribute : Attribute { }
[AliasForExport(typeof(object))]
[PartCreationPolicy(CreationPolicy.Any)] // Compliant, Export is present
class AClass1 { }

[InheritedExport(typeof(object))]
interface IMyInheritedExportInterface { }
[AliasForInheritedExport(typeof(object))]
[PartCreationPolicy(CreationPolicy.Any)] // Compliant, InheritanceExport is present
class AClass2 { }

interface IFoo { }
interface IBar { }
interface IQix { }
[AliasForExport(typeof(object))]
[AliasForPartCreationPolicy(CreationPolicy.Any)] // Compliant, Export is present
class AClass3 { }

[PartCreationPolicy(CreationPolicy.Any)] // Error [CS0116] - Compliant, illegal use, don't raise
[AliasForPartCreationPolicy(CreationPolicy.Any)] // Noncompliant
class AClass4 { }
}