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 @@ -12,10 +12,14 @@ public sealed class CultureInsensitiveTypeAttribute : System.Attribute
{
public CultureInsensitiveTypeAttribute() { }
public CultureInsensitiveTypeAttribute(string? format) => Format = format;
public CultureInsensitiveTypeAttribute(bool isDefaultFormatCultureInsensitive) => IsDefaultFormatCultureInsensitive = isDefaultFormatCultureInsensitive;
public CultureInsensitiveTypeAttribute(System.Type type) => Type = type;
public CultureInsensitiveTypeAttribute(System.Type type, string? format)
=> (Type, Format) = (type, format);
public CultureInsensitiveTypeAttribute(System.Type type, bool isDefaultFormatCultureInsensitive)
=> (Type, IsDefaultFormatCultureInsensitive) = (type, isDefaultFormatCultureInsensitive);

public Type? Type { get; }
public string? Format { get; }
public bool IsDefaultFormatCultureInsensitive { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>1.1.1</Version>
<Version>1.2.0</Version>
<Description>Annotations to configure Meziantou.Analyzer</Description>
<PackageTags>Meziantou.Analyzer, analyzers</PackageTags>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,36 @@ private bool IsCultureSensitiveType(ITypeSymbol? typeSymbol, CultureSensitiveOpt
if (!typeSymbol.Implements(SystemIFormattableSymbol))
return false;

if (!IsCultureSensitiveTypeUsingAttribute(typeSymbol, hasFormat: false, format: null))
if (!IsCultureSensitiveTypeUsingAttribute(typeSymbol))
return false;

return true;
}

private bool IsCultureSensitiveTypeUsingAttribute(ITypeSymbol typeSymbol)
{
var attributes = typeSymbol.GetAttributes(CultureInsensitiveTypeAttributeSymbol);
foreach (var attr in attributes)
{
if (attr.ConstructorArguments.IsEmpty)
return false; // no format is set, so the type is culture insensitive
}

foreach (var attribute in compilation.Assembly.GetAttributes(CultureInsensitiveTypeAttributeSymbol))
{
if (attribute.ConstructorArguments.IsEmpty)
continue;

if (attribute.ConstructorArguments[0].Value is INamedTypeSymbol attributeType && attributeType.IsEqualTo(typeSymbol))
{
if (attribute.ConstructorArguments.Length == 1)
return false;
}
}

return true;
}

private bool IsCultureSensitiveTypeUsingAttribute(ITypeSymbol typeSymbol, bool hasFormat, string? format)
{
var attributes = typeSymbol.GetAttributes(CultureInsensitiveTypeAttributeSymbol);
Expand All @@ -300,10 +324,16 @@ private bool IsCultureSensitiveTypeUsingAttribute(ITypeSymbol typeSymbol, bool h
if (attr.ConstructorArguments.IsEmpty)
return false; // no format is set, so the type is culture insensitive

var attrValue = attr.ConstructorArguments[0].Value;
if (!hasFormat)
{
if (attrValue is bool isDefaultFormatCultureInsensitive && isDefaultFormatCultureInsensitive)
return false;

continue;
}

var attrFormat = attr.ConstructorArguments[0].Value as string;
var attrFormat = attrValue as string;
if (attrFormat == format)
return false; // no format is set, so the type is culture insensitive
}
Expand All @@ -318,10 +348,16 @@ private bool IsCultureSensitiveTypeUsingAttribute(ITypeSymbol typeSymbol, bool h
if (attribute.ConstructorArguments.Length == 1)
return false;

var attrValue = attribute.ConstructorArguments[1].Value;
if (!hasFormat)
{
if (attrValue is bool isDefaultFormatCultureInsensitive && isDefaultFormatCultureInsensitive)
return false;

continue;
}

var attrFormat = attribute.ConstructorArguments[1].Value as string;
var attrFormat = attrValue as string;
if (attrFormat == format)
return false; // no format is set, so the type is culture insensitive
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,48 @@ await CreateProjectBuilder()
.ValidateAsync();
}

[Theory]
[InlineData(""" $"abc{new System.DateTime()}" """)]
[InlineData(""" $"abc[|{new System.DateTime():a}|]" """)]
public async Task IgnoreTypeUsingAssemblyAttribute_WithFormat_DefaultFormatInvariant(string content)
{
var sourceCode = $$"""
[assembly: Meziantou.Analyzer.Annotations.CultureInsensitiveTypeAttribute(typeof(System.DateTime), isDefaultFormatCultureInsensitive: true)]

class Test
{
void A()
{
_ = {{content}};
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Theory]
[InlineData(""" $"abc[|{new System.DateTime()}|]" """)]
[InlineData(""" $"abc[|{new System.DateTime():a}|]" """)]
public async Task IgnoreTypeUsingAssemblyAttribute_WithFormat_DefaultFormatCultureSensitive(string content)
{
var sourceCode = $$"""
[assembly: Meziantou.Analyzer.Annotations.CultureInsensitiveTypeAttribute(typeof(System.DateTime), isDefaultFormatCultureInsensitive: false)]

class Test
{
void A()
{
_ = {{content}};
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Fact]
public async Task IgnoreTypeUsingAssemblyAttribute_WithFormatNotMatchingAttribute()
{
Expand Down