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

Enable string search and comparison CA rules in Core #19796

Merged
merged 1 commit into from
Jan 10, 2024
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
3 changes: 3 additions & 0 deletions src/Core/src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
dotnet_analyzer_diagnostic.category-Performance.severity = warning

[*.cs]
dotnet_diagnostic.CA1307.severity = error
dotnet_diagnostic.CA1805.severity = error
dotnet_diagnostic.CA1806.severity = error
dotnet_diagnostic.CA1825.severity = error
Expand All @@ -15,3 +16,5 @@ dotnet_diagnostic.CA1846.severity = error
dotnet_diagnostic.CA1847.severity = error
dotnet_diagnostic.CA1859.severity = error
dotnet_diagnostic.CA1860.severity = error
dotnet_diagnostic.CA1865.severity = error
dotnet_diagnostic.CA2249.severity = error
6 changes: 3 additions & 3 deletions src/Core/src/Converters/CornerRadiusTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo?

if (strValue != null)
{
value = strValue.Trim();
if (strValue.IndexOf(",", StringComparison.Ordinal) != -1)
strValue = strValue.Trim();
if (strValue.ContainsChar(','))
{ //Xaml
var cornerRadius = strValue.Split(',');
if (cornerRadius.Length == 4
Expand All @@ -35,7 +35,7 @@ public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo?
&& double.TryParse(cornerRadius[0], NumberStyles.Number, CultureInfo.InvariantCulture, out double l))
return new CornerRadius(l);
}
else if (strValue.Trim().IndexOf(" ", StringComparison.Ordinal) != -1)
else if (strValue.ContainsChar(' '))
{ //CSS
var cornerRadius = strValue.Split(' ');
if (cornerRadius.Length == 2
Expand Down
4 changes: 2 additions & 2 deletions src/Core/src/Converters/ThicknessTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
{
strValue = strValue.Trim();

if (strValue.IndexOf(",", StringComparison.Ordinal) != -1)
if (strValue.ContainsChar(','))
{ //Xaml
var thickness = strValue.Split(',');
switch (thickness.Length)
Expand All @@ -43,7 +43,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
break;
}
}
else if (strValue.IndexOf(" ", StringComparison.Ordinal) != -1)
else if (strValue.ContainsChar(' '))
{ //CSS
var thickness = strValue.Split(' ');
switch (thickness.Length)
Expand Down
28 changes: 28 additions & 0 deletions src/Core/src/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace Microsoft.Maui
{
/// <summary>
/// Convenience extensions so we have a single place to accommodate the fact we have to support .NET Standard 2.0
/// </summary>
internal static class StringExtensions
{
public static bool ContainsChar(this string toSearch, char character)
{
#if NETSTANDARD2_0
return toSearch.Contains(character.ToString());
#else
return toSearch.Contains(character, StringComparison.Ordinal);
#endif
}

public static int IndexOfChar(this string toSearch, char character)
{
#if NETSTANDARD2_0
return toSearch.IndexOf(character);
#else
return toSearch.IndexOf(character, StringComparison.Ordinal);
#endif
}
}
}
6 changes: 3 additions & 3 deletions src/Core/src/Fonts/FontFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static FontFile FromString(string input)
{
_ = input ?? throw new ArgumentNullException(nameof(input));

var hashIndex = input.IndexOf("#", StringComparison.Ordinal);
var hashIndex = input.IndexOfChar('#');
//UWP names require Spaces. Sometimes people may use those, "CuteFont-Regular#Cute Font" should be "CuteFont-Regular#CuteFont"
var postScriptName = hashIndex > 0 ? input.Substring(hashIndex + 1)
#if NETSTANDARD2_0
Expand Down Expand Up @@ -98,7 +98,7 @@ static IEnumerable<string> GetFontName(string fontFamily)
{
_ = fontFamily ?? throw new ArgumentNullException(nameof(fontFamily));

if (fontFamily.IndexOf(" ", StringComparison.Ordinal) != -1)
if (fontFamily.ContainsChar(' '))
{
yield return fontFamily;
//We are done, they have spaces, they have it handled.
Expand All @@ -107,7 +107,7 @@ static IEnumerable<string> GetFontName(string fontFamily)

string currentString = "";
char lastCharacter = ' ';
var index = fontFamily.LastIndexOf("-", StringComparison.Ordinal);
var index = fontFamily.LastIndexOf('-');
bool multipleCaps = false;
var cleansedString = index > 0 ? fontFamily.Substring(0, index) : fontFamily;
foreach (var c in cleansedString)
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Fonts/FontManager.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ string FontNameToFontFile(string fontFamily)
{
fontFamily ??= string.Empty;

int hashtagIndex = fontFamily.IndexOf("#", StringComparison.Ordinal);
int hashtagIndex = fontFamily.IndexOf('#', StringComparison.Ordinal);
if (hashtagIndex >= 0)
return fontFamily.Substring(0, hashtagIndex);

Expand Down
4 changes: 2 additions & 2 deletions src/Core/src/Platform/iOS/TimePickerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public static void UpdateTime(this MauiTimePicker mauiTimePicker, ITimePicker ti

if (format != null)
{
if (format.IndexOf("H", StringComparison.Ordinal) != -1)
if (format.Contains('H', StringComparison.Ordinal))
{
var ci = new CultureInfo("de-DE");
NSLocale locale = new NSLocale(ci.TwoLetterISOLanguageName);

if (picker != null)
picker.Locale = locale;
}
else if (format.IndexOf("h", StringComparison.Ordinal) != -1)
else if (format.Contains('h', StringComparison.Ordinal))
{
var ci = new CultureInfo("en-US");
NSLocale locale = new NSLocale(ci.TwoLetterISOLanguageName);
Expand Down
Loading