Skip to content

Commit 8697f8d

Browse files
committed
Fix some static analysis warnings
1 parent 933ba9a commit 8697f8d

6 files changed

Lines changed: 62 additions & 49 deletions

File tree

src/DemystifyExceptions/Demystify/EnhancedStackTrace.Frames.cs

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Collections.Generic;
88
using System.Collections.Generic.Enumerable;
99
using System.Diagnostics.Internal;
10-
using System.IO;
1110
using System.Linq;
1211
using System.Reflection;
1312
using System.Runtime.CompilerServices;
@@ -25,7 +24,7 @@ private static List<EnhancedStackFrame> GetFrames(Exception exception)
2524
{
2625
if (exception == null) return new List<EnhancedStackFrame>();
2726

28-
var needFileInfo = true;
27+
const bool needFileInfo = true;
2928
var stackTrace = new StackTrace(exception, needFileInfo);
3029

3130
return GetFrames(stackTrace);
@@ -93,7 +92,7 @@ public static ResolvedMethod GetMethodDisplayString(MethodBase originMethod)
9392
// Method name
9493
methodDisplayInfo.MethodBase = method;
9594
methodDisplayInfo.Name = methodName;
96-
if (method.Name.IndexOf("<") >= 0)
95+
if (method.Name.IndexOf("<", StringComparison.Ordinal) >= 0)
9796
{
9897
if (TryResolveGeneratedName(ref method, out type, out methodName, out subMethodName, out var kind,
9998
out var ordinal))
@@ -126,7 +125,7 @@ public static ResolvedMethod GetMethodDisplayString(MethodBase originMethod)
126125
var value = field.GetValue(field);
127126
if (value is Delegate d)
128127
if (ReferenceEquals(d.Method, originMethod) &&
129-
d.Target.ToString() == originMethod.DeclaringType.ToString())
128+
d.Target.ToString() == originMethod.DeclaringType?.ToString())
130129
{
131130
methodDisplayInfo.Name = field.Name;
132131
methodDisplayInfo.IsLambda = false;
@@ -220,23 +219,26 @@ private static bool TryResolveGeneratedName(ref MethodBase method, out Type type
220219
switch (kind)
221220
{
222221
case GeneratedNameKind.LocalFunction:
223-
{
224-
var localNameStart = generatedName.IndexOf((char) kind, closeBracketOffset + 1);
225-
if (localNameStart < 0) break;
226-
localNameStart += 3;
227-
228-
if (localNameStart < generatedName.Length)
229222
{
230-
var localNameEnd = generatedName.IndexOf("|", localNameStart);
231-
if (localNameEnd > 0)
232-
subMethodName = generatedName.Substring(localNameStart, localNameEnd - localNameStart);
233-
}
223+
var localNameStart = generatedName.IndexOf((char)kind, closeBracketOffset + 1);
224+
if (localNameStart < 0) break;
225+
localNameStart += 3;
234226

235-
break;
236-
}
227+
if (localNameStart < generatedName.Length)
228+
{
229+
var localNameEnd = generatedName.IndexOf("|", localNameStart, StringComparison.Ordinal);
230+
if (localNameEnd > 0)
231+
subMethodName = generatedName.Substring(localNameStart, localNameEnd - localNameStart);
232+
}
233+
234+
break;
235+
}
237236
case GeneratedNameKind.LambdaMethod:
238237
subMethodName = generatedName;
239238
break;
239+
240+
default:
241+
break;
240242
}
241243

242244
var dt = method.DeclaringType;
@@ -338,10 +340,10 @@ private static bool TryResolveSourceMethod(IEnumerable<MethodBase> candidateMeth
338340

339341
private static void GetOrdinal(MethodBase method, ref int? ordinal)
340342
{
341-
var lamdaStart = method.Name.IndexOf((char) GeneratedNameKind.LambdaMethod + "__") + 3;
343+
var lamdaStart = method.Name.IndexOf((char)GeneratedNameKind.LambdaMethod + "__", StringComparison.Ordinal) + 3;
342344
if (lamdaStart > 3)
343345
{
344-
var secondStart = method.Name.IndexOf("_", lamdaStart) + 1;
346+
var secondStart = method.Name.IndexOf("_", lamdaStart, StringComparison.Ordinal) + 1;
345347
if (secondStart > 0) lamdaStart = secondStart;
346348

347349
if (!int.TryParse(method.Name.Substring(lamdaStart), out var foundOrdinal))
@@ -352,21 +354,22 @@ private static void GetOrdinal(MethodBase method, ref int? ordinal)
352354

353355
ordinal = foundOrdinal;
354356

355-
var methods = method.DeclaringType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic |
357+
var methods = method.DeclaringType?.GetMethods(BindingFlags.Public | BindingFlags.NonPublic |
356358
BindingFlags.Static | BindingFlags.Instance |
357359
BindingFlags.DeclaredOnly);
358360

359-
var startName = method.Name.Substring(0, lamdaStart);
360361
var count = 0;
361-
foreach (var m in methods)
362-
if (m.Name.Length > lamdaStart && m.Name.StartsWith(startName))
363-
{
364-
count++;
365-
366-
if (count > 1) break;
367-
}
368-
362+
if (methods != null)
363+
{
364+
var startName = method.Name.Substring(0, lamdaStart);
365+
foreach (var m in methods)
366+
if (m.Name.Length > lamdaStart && m.Name.StartsWith(startName))
367+
{
368+
count++;
369369

370+
if (count > 1) break;
371+
}
372+
}
370373
if (count <= 1) ordinal = null;
371374
}
372375
}
@@ -378,12 +381,15 @@ private static string GetMatchHint(GeneratedNameKind kind, MethodBase method)
378381
switch (kind)
379382
{
380383
case GeneratedNameKind.LocalFunction:
381-
var start = methodName.IndexOf("|");
384+
var start = methodName.IndexOf("|", StringComparison.Ordinal);
382385
if (start < 1) return null;
383-
var end = methodName.IndexOf("_", start) + 1;
386+
var end = methodName.IndexOf("_", start, StringComparison.Ordinal) + 1;
384387
if (end <= start) return null;
385388

386389
return methodName.Substring(start, end - start);
390+
391+
default:
392+
break;
387393
}
388394

389395
return null;
@@ -413,7 +419,7 @@ internal static bool TryParseGeneratedName(
413419
int c = name[closeBracketOffset + 1];
414420
if (c >= '1' && c <= '9' || c >= 'a' && c <= 'z') // Note '0' is not special.
415421
{
416-
kind = (GeneratedNameKind) c;
422+
kind = (GeneratedNameKind)c;
417423
return true;
418424
}
419425
}
@@ -619,6 +625,7 @@ private static bool TryResolveStateMachineMethod(ref MethodBase method, out Type
619625
Debug.Assert(method.DeclaringType != null);
620626

621627
declaringType = method.DeclaringType;
628+
if (declaringType == null) return false;
622629

623630
var parentType = declaringType.DeclaringType;
624631
if (parentType == null) return false;

src/DemystifyExceptions/Demystify/EnhancedStackTrace.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections;
55
using System.Collections.Generic;
66
using System.Collections.Generic.Enumerable;
7+
using System.Linq;
78
using System.Text;
89

910
namespace System.Diagnostics
@@ -78,7 +79,7 @@ public override StackFrame GetFrame(int index)
7879
/// </returns>
7980
public override StackFrame[] GetFrames()
8081
{
81-
return _frames.ToArray();
82+
return _frames.Cast<StackFrame>().ToArray();
8283
}
8384

8485
/// <summary>

src/MirrorInternalLogs/MirrorInternalLogsPatcher.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ bool IsUnityPlayer(ProcessModule p)
7979

8080
var proc = Process.GetCurrentProcess().Modules
8181
.Cast<ProcessModule>()
82-
.FirstOrDefault(IsUnityPlayer) ?? Process.GetCurrentProcess().MainModule;
82+
.FirstOrDefault(IsUnityPlayer) ?? Process.GetCurrentProcess().MainModule ?? throw new InvalidOperationException("Could not find Process.MainModule to patch");
8383

8484
if (IntPtr.Size == 8)
8585
patcher = new X64Patcher();
@@ -98,7 +98,7 @@ private static void InitializeFileLog()
9898
["process"] = () => Paths.ProcessName
9999
}));
100100
var dir = Path.GetDirectoryName(path);
101-
Directory.CreateDirectory(dir);
101+
Directory.CreateDirectory(dir ?? throw new InvalidOperationException("Path.GetDirectoryName is null for path: " + path));
102102
if (!TryCreateFile(path, out writer))
103103
{
104104
Logger.LogWarning(
@@ -111,7 +111,7 @@ private static void InitializeFileLog()
111111

112112
private static bool TryCreateFile(string path, out StreamWriter sw, int max = 50)
113113
{
114-
var dir = Path.GetDirectoryName(path);
114+
var dir = Path.GetDirectoryName(path) ?? throw new InvalidOperationException("Path.GetDirectoryName is null for path: " + path);
115115
var filename = Path.GetFileNameWithoutExtension(path);
116116
var ext = Path.GetExtension(path);
117117

src/MirrorInternalLogs/Util/InternalLogPatch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public InternalLogPatch(BytePattern pattern, T target)
2424
Pattern = pattern;
2525

2626
var delegateType = typeof(T);
27-
var invoke = delegateType.GetMethod("Invoke");
27+
var invoke = delegateType.GetMethod("Invoke") ?? throw new InvalidOperationException("delegateType.GetMethod(\"Invoke\") returned null for " + delegateType);
2828
var callParams = invoke.GetParameters();
2929

3030
var dmd = new DynamicMethodDefinition($"{typeof(InternalLogPatch<T>).FullName}_Detour", invoke.ReturnType,

src/ScriptEngine/ScriptEngine.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ public class ScriptEngine : BaseUnityPlugin
2424

2525
public string ScriptDirectory => Path.Combine(Paths.BepInExRootPath, "scripts");
2626

27-
GameObject scriptManager;
27+
private GameObject scriptManager;
2828

29-
ConfigEntry<bool> LoadOnStart { get; set; }
30-
ConfigEntry<KeyboardShortcut> ReloadKey { get; set; }
31-
ConfigEntry<bool> QuietMode { get; set; }
32-
ConfigEntry<bool> EnableFileSystemWatcher { get; set; }
33-
ConfigEntry<bool> IncludeSubdirectories { get; set; }
34-
ConfigEntry<float> AutoReloadDelay { get; set; }
29+
private ConfigEntry<bool> LoadOnStart { get; set; }
30+
private ConfigEntry<KeyboardShortcut> ReloadKey { get; set; }
31+
private ConfigEntry<bool> QuietMode { get; set; }
32+
private ConfigEntry<bool> EnableFileSystemWatcher { get; set; }
33+
private ConfigEntry<bool> IncludeSubdirectories { get; set; }
34+
private ConfigEntry<float> AutoReloadDelay { get; set; }
3535

3636
private FileSystemWatcher fileSystemWatcher;
3737
private bool shouldReload;
3838
private float autoReloadTimer;
3939

40-
void Awake()
40+
private void Awake()
4141
{
4242
LoadOnStart = Config.Bind("General", "LoadOnStart", false, new ConfigDescription("Load all plugins from the scripts folder when starting the application. This is done from inside of Chainloader's Awake, therefore not all plugis might be loaded yet. BepInDependency attributes are ignored."));
4343
ReloadKey = Config.Bind("General", "ReloadKey", new KeyboardShortcut(KeyCode.F6), new ConfigDescription("Press this key to reload all the plugins from the scripts folder"));
@@ -53,7 +53,7 @@ void Awake()
5353
StartFileSystemWatcher();
5454
}
5555

56-
void Update()
56+
private void Update()
5757
{
5858
if (ReloadKey.Value.IsDown())
5959
{
@@ -67,7 +67,7 @@ void Update()
6767
}
6868
}
6969

70-
void ReloadPlugins()
70+
private void ReloadPlugins()
7171
{
7272
shouldReload = false;
7373

@@ -104,7 +104,7 @@ void ReloadPlugins()
104104
}
105105
}
106106

107-
void LoadDLL(string path, GameObject obj)
107+
private void LoadDLL(string path, GameObject obj)
108108
{
109109
var defaultResolver = new DefaultAssemblyResolver();
110110
defaultResolver.AddSearchDirectory(ScriptDirectory);
@@ -215,7 +215,7 @@ private IEnumerable<Type> GetTypesSafe(Assembly ass)
215215
}
216216
}
217217

218-
IEnumerator DelayAction(Action action)
218+
private IEnumerator DelayAction(Action action)
219219
{
220220
yield return null;
221221
action();

src/SimpleProfiler/MonoProfilerController/MonoProfilerController.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ private void Update()
3434
if (_key.Value.IsDown())
3535
{
3636
var dumpFile = MonoProfilerPatcher.RunProfilerDump();
37+
3738
if (_uniqueNames.Value)
38-
dumpFile.MoveTo(Path.Combine(dumpFile.DirectoryName, $"{Path.GetFileNameWithoutExtension(dumpFile.Name)}_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}{dumpFile.Extension}"));
39+
{
40+
var containingDirectory = dumpFile.DirectoryName ?? throw new InvalidOperationException("dumpFile.DirectoryName is null for " + dumpFile);
41+
dumpFile.MoveTo(Path.Combine(containingDirectory, $"{Path.GetFileNameWithoutExtension(dumpFile.Name)}_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}{dumpFile.Extension}"));
42+
}
43+
3944
Logger.LogMessage("Saved profiler dump to " + dumpFile.FullName);
4045
}
4146
}

0 commit comments

Comments
 (0)