Skip to content

Commit

Permalink
Merge pull request #87518 from paulloz/dotnet/fix-warnings
Browse files Browse the repository at this point in the history
Clean a number of C# warnings
  • Loading branch information
akien-mga committed Jan 26, 2024
2 parents e96030e + 46b3096 commit 85b2c94
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue)]
public sealed class NotNullAttribute : Attribute
{
}
}
15 changes: 15 additions & 0 deletions modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace Godot.SourceGenerators
{
public static class Helper
{
[Conditional("DEBUG")]
public static void ThrowIfNull([NotNull] object? value)
{
_ = value ?? throw new ArgumentNullException();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -34,7 +33,7 @@ private void AnalyzeNode(SyntaxNodeAnalysisContext context)

// Method invocation or variable declaration that contained the type arguments
var parentSyntax = context.Node.Parent;
Debug.Assert(parentSyntax != null);
Helper.ThrowIfNull(parentSyntax);

var sm = context.SemanticModel;

Expand All @@ -49,9 +48,10 @@ private void AnalyzeNode(SyntaxNodeAnalysisContext context)
continue;

var typeSymbol = sm.GetSymbolInfo(typeSyntax).Symbol as ITypeSymbol;
Debug.Assert(typeSymbol != null);
Helper.ThrowIfNull(typeSymbol);

var parentSymbol = sm.GetSymbolInfo(parentSyntax).Symbol;
Helper.ThrowIfNull(parentSymbol);

if (!ShouldCheckTypeArgument(context, parentSyntax, parentSymbol, typeSyntax, typeSymbol, i))
{
Expand All @@ -69,7 +69,7 @@ private void AnalyzeNode(SyntaxNodeAnalysisContext context)

var marshalType = MarshalUtils.ConvertManagedTypeToMarshalType(typeSymbol, typeCache);

if (marshalType == null)
if (marshalType is null)
{
Common.ReportGenericTypeArgumentMustBeVariant(context, typeSyntax, typeSymbol);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void AppendPartialContainingTypeDeclarations(INamedTypeSymbol? containingType)
.Append(" /// </summary>\n");

source.Append(
$" public new class MethodName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.MethodName {{\n");
$" public new class MethodName : {symbol.BaseType!.FullQualifiedNameIncludeGlobal()}.MethodName {{\n");

// Generate cached StringNames for methods and properties, for fast lookup

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void AppendPartialContainingTypeDeclarations(INamedTypeSymbol? containingType)
.Append(" /// </summary>\n");

source.Append(
$" public new class PropertyName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.PropertyName {{\n");
$" public new class PropertyName : {symbol.BaseType!.FullQualifiedNameIncludeGlobal()}.PropertyName {{\n");

// Generate cached StringNames for methods and properties, for fast lookup

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void AppendPartialContainingTypeDeclarations(INamedTypeSymbol? containingType)
.Append(" /// </summary>\n");

source.Append(
$" public new class SignalName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.SignalName {{\n");
$" public new class SignalName : {symbol.BaseType!.FullQualifiedNameIncludeGlobal()}.SignalName {{\n");

// Generate cached StringNames for methods and properties, for fast lookup

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ private ProblemItem CreateProblemItem(BuildDiagnostic diagnostic, bool includeFi

public override void _Ready()
{
var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings();
var editorSettings = EditorInterface.Singleton.GetEditorSettings();
_layout = editorSettings.GetSetting(GodotSharpEditor.Settings.ProblemsLayout).As<ProblemsLayout>();

Name = "Problems".TTR();
Expand Down Expand Up @@ -655,7 +655,7 @@ public override void _Notification(int what)
switch ((long)what)
{
case EditorSettings.NotificationEditorSettingsChanged:
var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings();
var editorSettings = EditorInterface.Singleton.GetEditorSettings();
_layout = editorSettings.GetSetting(GodotSharpEditor.Settings.ProblemsLayout).As<ProblemsLayout>();
_toggleLayoutButton.ButtonPressed = GetToggleLayoutPressedState();
UpdateProblemsView();
Expand Down
6 changes: 2 additions & 4 deletions modules/mono/editor/bindings_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2237,10 +2237,6 @@ Error BindingsGenerator::_generate_cs_method(const BindingsGenerator::TypeInterf

p_output.append(INDENT1 "/// </summary>");
}

if (p_imethod.method_doc->is_deprecated) {
p_output.append(MEMBER_BEGIN "[Obsolete(\"This method is deprecated.\")]");
}
}

if (default_args_doc.get_string_length()) {
Expand All @@ -2255,6 +2251,8 @@ Error BindingsGenerator::_generate_cs_method(const BindingsGenerator::TypeInterf
p_output.append(MEMBER_BEGIN "[Obsolete(\"");
p_output.append(p_imethod.deprecation_message);
p_output.append("\")]");
} else if (p_imethod.method_doc && p_imethod.method_doc->is_deprecated) {
p_output.append(MEMBER_BEGIN "[Obsolete(\"This method is deprecated.\")]");
}

if (p_imethod.is_compat) {
Expand Down
3 changes: 3 additions & 0 deletions modules/mono/glue/GodotSharp/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ dotnet_diagnostic.CA1062.severity = error
dotnet_diagnostic.CA1069.severity = none
# CA1708: Identifiers should differ by more than case
dotnet_diagnostic.CA1708.severity = none
# CA1716: Identifiers should not match keywords
# This is suppressed, because it will report `@event` as well as `event`
dotnet_diagnostic.CA1716.severity = none
# CS1591: Missing XML comment for publicly visible type or member
dotnet_diagnostic.CS1591.severity = none
# CS1573: Parameter has no matching param tag in the XML comment
Expand Down
2 changes: 2 additions & 0 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ namespace Godot.Collections
/// interfacing with the engine. Otherwise prefer .NET collections
/// such as <see cref="System.Array"/> or <see cref="List{T}"/>.
/// </summary>
#pragma warning disable CA1710 // Identifiers should have correct suffix
public sealed class Array :
#pragma warning restore CA1710
IList<Variant>,
IReadOnlyList<Variant>,
ICollection,
Expand Down
25 changes: 12 additions & 13 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Text;
Expand Down Expand Up @@ -220,7 +219,7 @@ public static string Dedent(this string instance)
{
if (hasText)
{
sb.Append(instance.Substring(indentStop, i - indentStop));
sb.Append(instance.AsSpan(indentStop, i - indentStop));
}
sb.Append('\n');
hasText = false;
Expand Down Expand Up @@ -252,7 +251,7 @@ public static string Dedent(this string instance)

if (hasText)
{
sb.Append(instance.Substring(indentStop, instance.Length - indentStop));
sb.Append(instance.AsSpan(indentStop, instance.Length - indentStop));
}

return sb.ToString();
Expand Down Expand Up @@ -323,7 +322,7 @@ public static string Capitalize(this string instance)
string slice = aux.GetSliceCharacter(' ', i);
if (slice.Length > 0)
{
slice = char.ToUpper(slice[0]) + slice.Substring(1);
slice = char.ToUpperInvariant(slice[0]) + slice.Substring(1);
if (i > 0)
cap += " ";
cap += slice;
Expand Down Expand Up @@ -408,13 +407,13 @@ private static string CamelcaseToUnderscore(this string instance, bool lowerCase
bool shouldSplit = condA || condB || condC || canBreakNumberLetter || canBreakLetterNumber;
if (shouldSplit)
{
newString += instance.Substring(startIndex, i - startIndex) + "_";
newString += string.Concat(instance.AsSpan(startIndex, i - startIndex), "_");
startIndex = i;
}
}

newString += instance.Substring(startIndex, instance.Length - startIndex);
return lowerCase ? newString.ToLower() : newString;
return lowerCase ? newString.ToLowerInvariant() : newString;
}

/// <summary>
Expand Down Expand Up @@ -479,9 +478,9 @@ public static int CompareTo(this string instance, string to, bool caseSensitive
return -1; // If this is empty, and the other one is not, then we're less... I think?
if (to[toIndex] == 0)
return 1; // Otherwise the other one is smaller..
if (char.ToUpper(instance[instanceIndex]) < char.ToUpper(to[toIndex])) // More than
if (char.ToUpperInvariant(instance[instanceIndex]) < char.ToUpperInvariant(to[toIndex])) // More than
return -1;
if (char.ToUpper(instance[instanceIndex]) > char.ToUpper(to[toIndex])) // Less than
if (char.ToUpperInvariant(instance[instanceIndex]) > char.ToUpperInvariant(to[toIndex])) // Less than
return 1;

instanceIndex++;
Expand Down Expand Up @@ -853,15 +852,15 @@ public static string Indent(this string instance, string prefix)
else
{
sb.Append(prefix);
sb.Append(instance.Substring(lineStart, i - lineStart + 1));
sb.Append(instance.AsSpan(lineStart, i - lineStart + 1));
}
lineStart = i + 1;
}
}
if (lineStart != instance.Length)
{
sb.Append(prefix);
sb.Append(instance.Substring(lineStart));
sb.Append(instance.AsSpan(lineStart));
}
return sb.ToString();
}
Expand Down Expand Up @@ -925,8 +924,8 @@ public static bool IsSubsequenceOf(this string instance, string text, bool caseS

if (!caseSensitive)
{
char sourcec = char.ToLower(instance[source]);
char targetc = char.ToLower(text[target]);
char sourcec = char.ToLowerInvariant(instance[source]);
char targetc = char.ToLowerInvariant(text[target]);
match = sourcec == targetc;
}
else
Expand Down Expand Up @@ -1234,7 +1233,7 @@ private static bool ExprMatch(this string instance, string expr, bool caseSensit
return false;
if (caseSensitive)
return instance[0] == expr[0];
return (char.ToUpper(instance[0]) == char.ToUpper(expr[0])) &&
return (char.ToUpperInvariant(instance[0]) == char.ToUpperInvariant(expr[0])) &&
ExprMatch(instance.Substring(1), expr.Substring(1), caseSensitive);
}
}
Expand Down

0 comments on commit 85b2c94

Please sign in to comment.