Skip to content

Commit

Permalink
Run code analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Feb 21, 2024
1 parent 5cdb4c4 commit bebd8e1
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/cscore/UsbCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public UsbCamera(string name, string path) : base(CreateUsbCameraPath(name, path

public static UsbCameraInfo[] EnumerateUsbCameras()
{
var info = CsNative.EnumerateUsbCameras(out var status);
var info = CsNative.EnumerateUsbCameras();

return info;
}
Expand Down
12 changes: 3 additions & 9 deletions src/cscore/VideoEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@ namespace CsCore;

[NativeMarshalling(typeof(VideoEventMarshaller))]
[StructLayout(LayoutKind.Auto)]
public readonly struct VideoEvent : INativeArrayFree<VideoEventMarshaller.NativeCsEvent>
public readonly struct VideoEvent(in VideoEventMarshaller.NativeCsEvent csEvent) : INativeArrayFree<VideoEventMarshaller.NativeCsEvent>
{
public EventKind Kind { get; }
public CsListener Listener { get; }

public VideoEvent(in VideoEventMarshaller.NativeCsEvent csEvent)
{
Kind = csEvent.kind;
Listener = new CsListener(csEvent.listener);
}
public EventKind Kind { get; } = csEvent.kind;
public CsListener Listener { get; } = new CsListener(csEvent.listener);

public static unsafe void FreeArray(VideoEventMarshaller.NativeCsEvent* ptr, int len)
{
Expand Down
1 change: 1 addition & 0 deletions src/cscore/VideoListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public VideoListener(Action<VideoEvent> listener, EventKind eventMask, bool imme

public void Dispose()
{
GC.SuppressFinalize(this);
if (Handle.Handle != 0)
{
lock (s_listenerLock)
Expand Down
3 changes: 2 additions & 1 deletion src/cscore/VideoSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public VideoProperty GetSourceProperty(string name)

public static VideoSink[] EnumerateSinks()
{
var handles = CsNative.EnumerateSinks(out var status);
var handles = CsNative.EnumerateSinks();

VideoSink[] rv = new VideoSink[handles.Length];
for (int i = 0; i < handles.Length; i++)
Expand All @@ -122,6 +122,7 @@ public static VideoSink[] EnumerateSinks()

public void Dispose()
{
GC.SuppressFinalize(this);
if (Handle.Handle != 0)
{
CsNative.ReleaseSink(Handle);
Expand Down
3 changes: 2 additions & 1 deletion src/cscore/VideoSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public VideoProperty[] EnumerateProperties()

public static VideoSource[] EnumerateSources()
{
var handles = CsNative.EnumerateSources(out var status);
var handles = CsNative.EnumerateSources();

VideoSource[] rv = new VideoSource[handles.Length];
for (int i = 0; i < handles.Length; i++)
Expand All @@ -176,6 +176,7 @@ public static VideoSource[] EnumerateSources()

public void Dispose()
{
GC.SuppressFinalize(this);
if (Handle.Handle != 0)
{
CsNative.ReleaseSource(Handle);
Expand Down
2 changes: 1 addition & 1 deletion src/hal/Natives/HalI2C.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static Span<byte> ReadI2C(I2CPort port, int deviceAddress, Span<byte> buf
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int TransactionI2C(I2CPort port, int deviceAddress, ReadOnlySpan<byte> dataToSend, int sendSize, Span<byte> dataReceived, int receiveSize);

public static Span<byte> TransactionI2C(I2CPort port, int deviceAddress, ReadOnlySpan<byte> dataToSend, int sendSize, Span<byte> dataReceived)
public static Span<byte> TransactionI2C(I2CPort port, int deviceAddress, ReadOnlySpan<byte> dataToSend, Span<byte> dataReceived)
{
int read = TransactionI2C(port, deviceAddress, dataToSend, dataToSend.Length, dataReceived, dataReceived.Length);
if (read > 0)
Expand Down
2 changes: 2 additions & 0 deletions src/ntcore/Natives/RefNetworkTableValueMarshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public unsafe ref struct RefNetworkTableValueMarshaller
public static int BufferSize => 256;
private ref readonly byte m_toPin;

#pragma warning disable IDE0052 // Remove unread private members
private ref Ptr<byte> m_toAssignPin;
#pragma warning restore IDE0052 // Remove unread private members

private NetworkTableValueMarshaller.NativeNetworkTableValue m_nativeValue;

Expand Down
23 changes: 22 additions & 1 deletion src/thirdparty/Stereologue/Stereologuer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Google.Protobuf.WellKnownTypes;
using NetworkTables;
using WPIUtil.Logging;
using WPIUtil.Serialization.Protobuf;
Expand Down Expand Up @@ -186,11 +187,21 @@ public void LogChar(string path, LogType logType, char value, LogLevel logLevel

public void LogFloat(string path, LogType logType, float value, LogLevel logLevel = LogLevel.Default)
{
ref var logs = ref CheckDoLog(path, ref logType, logLevel, doubleLogs);
ref var logs = ref CheckDoLog(path, ref logType, logLevel, floatLogs);
if (Unsafe.IsNullRef(ref logs))
{
return;
}
if (logType.HasFlag(LogType.Nt))
{
logs.topic ??= ntInstance.GetFloatTopic(path).Publish(PubSubOptions.None);
logs.topic.Set(value);
}
if (logType.HasFlag(LogType.File))
{
logs.logEntry ??= new FloatLogEntry(log, path);
logs.logEntry.Append(value);
}
}

public void LogDouble(string path, LogType logType, double value, LogLevel logLevel = LogLevel.Default)
Expand All @@ -200,6 +211,16 @@ public void LogDouble(string path, LogType logType, double value, LogLevel logLe
{
return;
}
if (logType.HasFlag(LogType.Nt))
{
logs.topic ??= ntInstance.GetDoubleTopic(path).Publish(PubSubOptions.None);
logs.topic.Set(value);
}
if (logType.HasFlag(LogType.File))
{
logs.logEntry ??= new DoubleLogEntry(log, path);
logs.logEntry.Append(value);
}
}

public void LogString(string path, LogType logType, string value, LogLevel logLevel = LogLevel.Default)
Expand Down
2 changes: 1 addition & 1 deletion src/wpiutil/Marshal/WPIStringMarshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public readonly ref readonly byte GetPinnableReference()
return (WpiStringNative*)Unsafe.AsPointer(ref nativeString);
}

public void Free()
public readonly void Free()
{
// Purposely empty
}
Expand Down
2 changes: 1 addition & 1 deletion src/wpiutil/Natives/RawFrameNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public readonly ref readonly byte GetPinnableReference()
return (NativeRawFrame*)Unsafe.AsPointer(ref rawFrame);
}

public void Free()
public readonly void Free()
{
}
}
Expand Down
26 changes: 24 additions & 2 deletions src/wpiutil/Sendable/SendableRegistery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,29 @@ public static void AddLW(ISendable sendable, string name)
}
}

public static void AddLW(ISendable sendable, string name, string moduleType, int moduleNumber, int channel)
public static void AddLW(ISendable sendable, string moduleType, int channel)
{
lock (s_lockObject)
{
Component comp = GetOrAdd(sendable);
if (liveWindowFactory is not null)
{
try
{
comp.m_builder?.Dispose();
}
catch
{
// Ignore
}
comp.m_builder = liveWindowFactory();
}
comp.m_liveWindow = true;
comp.SetName(moduleType, channel);
}
}

public static void AddLW(ISendable sendable, string moduleType, int moduleNumber, int channel)
{
lock (s_lockObject)
{
Expand Down Expand Up @@ -423,7 +445,7 @@ public class CallbackData
public ISendableBuilder? Builder { get; internal set; }
}

private static List<Component> ForeachComponents = [];
private static readonly List<Component> ForeachComponents = [];

public static void ForeachLiveWindow(int dataHandle, Action<CallbackData> callback)
{
Expand Down
2 changes: 1 addition & 1 deletion src/wpiutil/Serialization/Struct/DynamicStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public string GetStringField(StructFieldDescriptor field)
}
}

OperationStatus result = Rune.DecodeLastFromUtf8(bytes[..stringLength], out var lastChar, out var consumed);
OperationStatus result = Rune.DecodeLastFromUtf8(bytes[..stringLength], out _, out var consumed);
#pragma warning disable CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
ReadOnlySpan<byte> buffer = result switch
{
Expand Down
2 changes: 1 addition & 1 deletion src/wpiutil/Serialization/Struct/Parsing/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public ref struct Lexer(ReadOnlySpan<byte> inStr)
{
private Utf8CodePointEnumerator m_enumerator = new(inStr);

public int Position => m_enumerator.CurrentMark;
public readonly int Position => m_enumerator.CurrentMark;

public TokenKind Scan()
{
Expand Down
2 changes: 1 addition & 1 deletion src/wpiutil/Serialization/Struct/Parsing/ParsedSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace WPIUtil.Serialization.Struct.Parsing;

public sealed class ParsedSchema()
{
private List<ParsedDeclaration> m_declarations = [];
private readonly List<ParsedDeclaration> m_declarations = [];

public ReadOnlySpan<ParsedDeclaration> Declarations => CollectionsMarshal.AsSpan(m_declarations);

Expand Down

0 comments on commit bebd8e1

Please sign in to comment.