more ref-like parameters

This commit is contained in:
Lightczx
2023-04-18 21:57:13 +08:00
parent 89fe93b3eb
commit aee5271a2d
26 changed files with 82 additions and 82 deletions

View File

@@ -172,4 +172,4 @@ internal sealed class IdentityGenerator : IIncrementalGenerator
/// </summary>
public string? Documentation { get; set; }
}
}
}

View File

@@ -49,7 +49,6 @@ internal sealed class UniversalAnalyzer : DiagnosticAnalyzer
context.RegisterSyntaxNodeAction(HandleTypeDeclaration, types);
context.RegisterSyntaxNodeAction(CollectReadOnlyStruct, SyntaxKind.StructDeclaration);
context.RegisterSyntaxNodeAction(HandleMethodDeclaration, SyntaxKind.MethodDeclaration);
}
@@ -87,21 +86,6 @@ internal sealed class UniversalAnalyzer : DiagnosticAnalyzer
}
}
private readonly HashSet<string> readOnlyStructs = new();
private void CollectReadOnlyStruct(SyntaxNodeAnalysisContext context)
{
StructDeclarationSyntax structSyntax = (StructDeclarationSyntax)context.Node;
if (structSyntax.Modifiers.Any(token => token.IsKind(SyntaxKind.ReadOnlyKeyword)))
{
if (context.SemanticModel.GetDeclaredSymbol(structSyntax) is INamedTypeSymbol symbol)
{
readOnlyStructs.Add(symbol.ToDisplayString());
}
}
}
private void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)
{
MethodDeclarationSyntax methodSyntax = (MethodDeclarationSyntax)context.Node;
@@ -112,6 +96,12 @@ internal sealed class UniversalAnalyzer : DiagnosticAnalyzer
return;
}
// 跳过重载方法
if (methodSyntax.Modifiers.Any(token => token.IsKind(SyntaxKind.OverrideKeyword)))
{
return;
}
// 跳过方法定义 如 接口
if (methodSyntax.Body == null)
{
@@ -122,7 +112,18 @@ internal sealed class UniversalAnalyzer : DiagnosticAnalyzer
{
if (context.SemanticModel.GetDeclaredSymbol(parameter) is IParameterSymbol symbol)
{
if (readOnlyStructs.Contains(symbol.Type.ToDisplayString()) && symbol.RefKind == RefKind.None)
if (IsBuiltInType(symbol.Type))
{
continue;
}
// 跳过 CancellationToken
if (symbol.Type.ToDisplayString() == "System.Threading.CancellationToken")
{
continue;
}
if (symbol.Type.IsReadOnly && symbol.RefKind == RefKind.None)
{
Location location = parameter.GetLocation();
Diagnostic diagnostic = Diagnostic.Create(readOnlyStructRefDescriptor, location, symbol.Type);
@@ -131,4 +132,27 @@ internal sealed class UniversalAnalyzer : DiagnosticAnalyzer
}
}
}
private bool IsBuiltInType(ITypeSymbol symbol)
{
return symbol.SpecialType switch
{
SpecialType.System_Boolean => true,
SpecialType.System_Char => true,
SpecialType.System_SByte => true,
SpecialType.System_Byte => true,
SpecialType.System_Int16 => true,
SpecialType.System_UInt16 => true,
SpecialType.System_Int32 => true,
SpecialType.System_UInt32 => true,
SpecialType.System_Int64 => true,
SpecialType.System_UInt64 => true,
SpecialType.System_Decimal => true,
SpecialType.System_Single => true,
SpecialType.System_Double => true,
SpecialType.System_IntPtr => true,
SpecialType.System_UIntPtr => true,
_ => false,
};
}
}

View File

@@ -60,7 +60,7 @@ internal sealed class DescriptionTextBlock : ContentControl
ApplyDescription(text, description);
}
private static void ApplyDescription(TextBlock text, ReadOnlySpan<char> description)
private static void ApplyDescription(TextBlock text, in ReadOnlySpan<char> description)
{
text.Inlines.Clear();
@@ -111,12 +111,12 @@ internal sealed class DescriptionTextBlock : ContentControl
}
}
private static void AppendText(TextBlock text, ReadOnlySpan<char> slice)
private static void AppendText(TextBlock text, in ReadOnlySpan<char> slice)
{
text.Inlines.Add(new Run { Text = slice.ToString() });
}
private static void AppendColorText(TextBlock text, ReadOnlySpan<char> slice, Rgba8 color)
private static void AppendColorText(TextBlock text, in ReadOnlySpan<char> slice, Rgba8 color)
{
Color targetColor;
if (ThemeHelper.IsDarkMode(text.ActualTheme))
@@ -137,7 +137,7 @@ internal sealed class DescriptionTextBlock : ContentControl
});
}
private static void AppendItalicText(TextBlock text, ReadOnlySpan<char> slice)
private static void AppendItalicText(TextBlock text, in ReadOnlySpan<char> slice)
{
text.Inlines.Add(new Run
{

View File

@@ -1,33 +0,0 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
namespace Snap.Hutao.Core.Abstraction;
[HighQuality]
[SuppressMessage("", "SA1600")]
internal abstract class DisposableObject : IDisposable
{
public bool IsDisposed { get; private set; }
public void Dispose()
{
if (!IsDisposed)
{
GC.SuppressFinalize(this);
Dispose(isDisposing: true);
}
}
protected virtual void Dispose(bool isDisposing)
{
IsDisposed = true;
}
protected void VerifyNotDisposed()
{
if (IsDisposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
}
}

View File

@@ -73,6 +73,7 @@ internal sealed class TempFileStream : Stream
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
stream.Dispose();
File.Delete(path);
}

View File

@@ -85,19 +85,19 @@ internal static class LocalSetting
}
/// <inheritdoc cref="Get{T}(string, T)"/>
public static DateTimeOffset Get(string key, DateTimeOffset defaultValue)
public static DateTimeOffset Get(string key, in DateTimeOffset defaultValue)
{
return Get<DateTimeOffset>(key, defaultValue);
}
/// <inheritdoc cref="Get{T}(string, T)"/>
public static TimeSpan Get(string key, TimeSpan defaultValue)
public static TimeSpan Get(string key, in TimeSpan defaultValue)
{
return Get<TimeSpan>(key, defaultValue);
}
/// <inheritdoc cref="Get{T}(string, T)"/>
public static Guid Get(string key, Guid defaultValue)
public static Guid Get(string key, in Guid defaultValue)
{
return Get<Guid>(key, defaultValue);
}
@@ -187,19 +187,19 @@ internal static class LocalSetting
}
/// <inheritdoc cref="Set{T}(string, T)"/>
public static void Set(string key, DateTimeOffset value)
public static void Set(string key, in DateTimeOffset value)
{
Set<DateTimeOffset>(key, value);
}
/// <inheritdoc cref="Set{T}(string, T)"/>
public static void Set(string key, TimeSpan value)
public static void Set(string key, in TimeSpan value)
{
Set<TimeSpan>(key, value);
}
/// <inheritdoc cref="Set{T}(string, T)"/>
public static void Set(string key, Guid value)
public static void Set(string key, in Guid value)
{
Set<Guid>(key, value);
}

View File

@@ -68,7 +68,7 @@ internal static class Persistence
/// </summary>
/// <param name="hwnd">窗体句柄</param>
/// <returns>缩放比</returns>
public static double GetScaleForWindowHandle(HWND hwnd)
public static double GetScaleForWindowHandle(in HWND hwnd)
{
uint dpi = GetDpiForWindow(hwnd);
return Math.Round(dpi / 96D, 2, MidpointRounding.AwayFromZero);

View File

@@ -75,6 +75,7 @@ internal sealed class WindowSubclass<TWindow> : IDisposable
}
}
[SuppressMessage("", "SH002")]
private unsafe LRESULT OnSubclassProcedure(HWND hwnd, uint uMsg, WPARAM wParam, LPARAM lParam, nuint uIdSubclass, nuint dwRefData)
{
switch (uMsg)
@@ -96,6 +97,7 @@ internal sealed class WindowSubclass<TWindow> : IDisposable
return DefSubclassProc(hwnd, uMsg, wParam, lParam);
}
[SuppressMessage("", "SH002")]
private LRESULT OnDragBarProcedure(HWND hwnd, uint uMsg, WPARAM wParam, LPARAM lParam, nuint uIdSubclass, nuint dwRefData)
{
switch (uMsg)

View File

@@ -15,7 +15,7 @@ internal static class DateTimeOffsetExtension
/// <param name="timestamp">时间戳</param>
/// <param name="defaultValue">默认值</param>
/// <returns>转换的时间</returns>
public static DateTimeOffset FromUnixTime(long? timestamp, DateTimeOffset defaultValue)
public static DateTimeOffset FromUnixTime(long? timestamp, in DateTimeOffset defaultValue)
{
if (timestamp is long value)
{

View File

@@ -17,7 +17,7 @@ internal static class NumberExtension
/// <param name="x">给定的整数</param>
/// <returns>位数</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Place(this int x)
public static int Place(in this int x)
{
// Benchmarked and compared as a most optimized solution
return (int)(MathF.Log10(x) + 1);

View File

@@ -59,7 +59,7 @@ internal sealed class Achievement : IEquatable<Achievement>
/// <param name="userId">对应的用户id</param>
/// <param name="id">成就Id</param>
/// <returns>新创建的成就</returns>
public static Achievement Create(Guid userId, int id)
public static Achievement Create(in Guid userId, int id)
{
return new()
{
@@ -76,7 +76,7 @@ internal sealed class Achievement : IEquatable<Achievement>
/// <param name="userId">对应的用户id</param>
/// <param name="uiaf">uiaf项</param>
/// <returns>新创建的成就</returns>
public static Achievement Create(Guid userId, UIAFItem uiaf)
public static Achievement Create(in Guid userId, UIAFItem uiaf)
{
return new()
{

View File

@@ -49,7 +49,7 @@ internal sealed class CultivateEntry
/// <param name="type">类型</param>
/// <param name="id">主Id</param>
/// <returns>养成入口点</returns>
public static CultivateEntry Create(Guid projectId, CultivateType type, int id)
public static CultivateEntry Create(in Guid projectId, CultivateType type, int id)
{
return new()
{

View File

@@ -53,7 +53,7 @@ internal sealed class CultivateItem
/// <param name="itemId">物品 Id</param>
/// <param name="count">个数</param>
/// <returns>养成物品</returns>
public static CultivateItem Create(Guid entryId, int itemId, int count)
public static CultivateItem Create(in Guid entryId, int itemId, int count)
{
return new()
{

View File

@@ -79,7 +79,7 @@ internal sealed class GachaItem
/// <param name="item">祈愿物品</param>
/// <param name="itemId">物品Id</param>
/// <returns>新的祈愿物品</returns>
public static GachaItem Create(Guid archiveId, GachaLogItem item, int itemId)
public static GachaItem Create(in Guid archiveId, GachaLogItem item, int itemId)
{
return new()
{
@@ -99,7 +99,7 @@ internal sealed class GachaItem
/// <param name="item">祈愿物品</param>
/// <param name="itemId">物品Id</param>
/// <returns>新的祈愿物品</returns>
public static GachaItem Create(Guid archiveId, UIGFItem item, int itemId)
public static GachaItem Create(in Guid archiveId, UIGFItem item, int itemId)
{
return new()
{
@@ -118,7 +118,7 @@ internal sealed class GachaItem
/// <param name="archiveId">存档Id</param>
/// <param name="item">祈愿物品</param>
/// <returns>新的祈愿物品</returns>
public static GachaItem Create(Guid archiveId, Web.Hutao.GachaLog.GachaItem item)
public static GachaItem Create(in Guid archiveId, Web.Hutao.GachaLog.GachaItem item)
{
return new()
{

View File

@@ -47,7 +47,7 @@ internal sealed class InventoryItem
/// <param name="projectId">项目Id</param>
/// <param name="itemId">物品Id</param>
/// <returns>新的个数为0的物品</returns>
public static InventoryItem Create(Guid projectId, int itemId)
public static InventoryItem Create(in Guid projectId, int itemId)
{
return new()
{

View File

@@ -24,5 +24,5 @@ internal interface ISummaryItemSource
/// <param name="time">时间</param>
/// <param name="isUp">是否为Up物品</param>
/// <returns>简述统计物品</returns>
SummaryItem ToSummaryItem(int lastPull, DateTimeOffset time, bool isUp);
SummaryItem ToSummaryItem(int lastPull, in DateTimeOffset time, bool isUp);
}

View File

@@ -75,7 +75,7 @@ internal partial class Avatar : IStatisticsItemSource, ISummaryItemSource, IName
}
/// <inheritdoc/>
public SummaryItem ToSummaryItem(int lastPull, DateTimeOffset time, bool isUp)
public SummaryItem ToSummaryItem(int lastPull, in DateTimeOffset time, bool isUp)
{
return new()
{

View File

@@ -78,7 +78,7 @@ internal sealed partial class Weapon : IStatisticsItemSource, ISummaryItemSource
/// <param name="time">时间</param>
/// <param name="isUp">是否为Up物品</param>
/// <returns>简述统计物品</returns>
public SummaryItem ToSummaryItem(int lastPull, DateTimeOffset time, bool isUp)
public SummaryItem ToSummaryItem(int lastPull, in DateTimeOffset time, bool isUp)
{
return new()
{

View File

@@ -33,6 +33,7 @@ internal sealed class AchievementDbOperation
/// <param name="items">待合并的项</param>
/// <param name="aggressive">是否贪婪</param>
/// <returns>导入结果</returns>
[SuppressMessage("", "SH002")]
public ImportResult Merge(Guid archiveId, IEnumerable<UIAFItem> items, bool aggressive)
{
IOrderedQueryable<EntityAchievement> oldData = appDbContext.Achievements
@@ -115,6 +116,7 @@ internal sealed class AchievementDbOperation
/// <param name="archiveId">成就id</param>
/// <param name="items">待覆盖的项</param>
/// <returns>导入结果</returns>
[SuppressMessage("", "SH002")]
public ImportResult Overwrite(Guid archiveId, IEnumerable<EntityAchievement> items)
{
IOrderedQueryable<EntityAchievement> oldData = appDbContext.Achievements

View File

@@ -324,6 +324,7 @@ internal sealed class CultivationService : ICultivationService
return true;
}
[SuppressMessage("", "SH002")]
private static Task<List<InventoryItem>> GetProjectInventoryAsync(AppDbContext appDbContext, Guid projectId)
{
return appDbContext.InventoryItems
@@ -332,6 +333,7 @@ internal sealed class CultivationService : ICultivationService
.ToListAsync();
}
[SuppressMessage("", "SH002")]
private static Task<List<CultivateEntry>> GetProjectEntriesAsync(AppDbContext appDbContext, Guid projectId)
{
return appDbContext.CultivateEntries
@@ -340,6 +342,7 @@ internal sealed class CultivationService : ICultivationService
.ToListAsync();
}
[SuppressMessage("", "SH002")]
private static Task<List<CultivateItem>> GetEntryItemsAsync(AppDbContext appDbContext, Guid entryId)
{
return appDbContext.CultivateItems

View File

@@ -19,7 +19,7 @@ internal static class GachaStatisticsExtension
/// </summary>
/// <param name="span">跨度</param>
/// <returns>平均值</returns>
public static byte Average(this Span<byte> span)
public static byte Average(this in Span<byte> span)
{
int sum = 0;
int count = 0;

View File

@@ -170,7 +170,7 @@ internal sealed class TypedWishSummaryBuilder
}
}
private void TrackFromToTime(DateTimeOffset time)
private void TrackFromToTime(in DateTimeOffset time)
{
if (time < fromTimeTracker)
{

View File

@@ -101,7 +101,7 @@ internal sealed class GameFpsUnlocker : IGameFpsUnlocker
}
}
private static unsafe MODULEENTRY32 UnsafeFindModule(int processId, ReadOnlySpan<byte> moduleName)
private static unsafe MODULEENTRY32 UnsafeFindModule(int processId, in ReadOnlySpan<byte> moduleName)
{
HANDLE snapshot = CreateToolhelp32Snapshot(CREATE_TOOLHELP_SNAPSHOT_FLAGS.TH32CS_SNAPMODULE, (uint)processId);
try

View File

@@ -208,7 +208,7 @@ internal static class ApiEndpoints
/// <param name="avatarId">角色Id</param>
/// <param name="uid">uid</param>
/// <returns>角色详情</returns>
public static string CalculateSyncAvatarDetail(AvatarId avatarId, in PlayerUid uid)
public static string CalculateSyncAvatarDetail(in AvatarId avatarId, in PlayerUid uid)
{
return $"{ApiTakumiEventCalculate}/v1/sync/avatar/detail?avatar_id={avatarId.Value}&uid={uid.Value}&region={uid.Region}";
}

View File

@@ -208,7 +208,7 @@ internal static class ApiOsEndpoints
/// <param name="avatarId">角色Id</param>
/// <param name="uid">uid</param>
/// <returns>角色详情</returns>
public static string CalculateSyncAvatarDetail(AvatarId avatarId, in PlayerUid uid)
public static string CalculateSyncAvatarDetail(in AvatarId avatarId, in PlayerUid uid)
{
return $"{SgPublicApi}/event/calculateos/sync/avatar/detail?avatar_id={avatarId.Value}&uid={uid.Value}&region={uid.Region}";
}

View File

@@ -85,6 +85,7 @@ internal static class StructMarshal
/// </summary>
/// <param name="snapshot">快照</param>
/// <returns>模块枚举</returns>
[SuppressMessage("", "SH002")]
public static IEnumerable<MODULEENTRY32> EnumerateModuleEntry32(HANDLE snapshot)
{
MODULEENTRY32 entry = MODULEENTRY32();
@@ -111,7 +112,7 @@ internal static class StructMarshal
return moduleEntry32.dwSize == 0;
}
private static unsafe BOOL UnsafeModule32First(HANDLE snapshot, ref MODULEENTRY32 lpme)
private static unsafe BOOL UnsafeModule32First(in HANDLE snapshot, ref MODULEENTRY32 lpme)
{
fixed (MODULEENTRY32* lpmeLocal = &lpme)
{
@@ -119,7 +120,7 @@ internal static class StructMarshal
}
}
private static unsafe BOOL UnsafeModule32Next(HANDLE snapshot, ref MODULEENTRY32 lpme)
private static unsafe BOOL UnsafeModule32Next(in HANDLE snapshot, ref MODULEENTRY32 lpme)
{
fixed (MODULEENTRY32* lpmeLocal = &lpme)
{