Skip to content

Commit 10304cd

Browse files
committed
* fix some violations of Roslyn analyzer rules
* level up `AnalysisMode` and enable `EnforceCodeStyleInBuild` @ Directory.Build.props * suppress other violations of Roslyn analyzer rules @ GlobalSuppressions.cs @ c#
1 parent 2028d24 commit 10304cd

16 files changed

+43
-27
lines changed

c#/.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ dotnet_naming_rule.constants_rule.import_to_resharper = as_predefined
8181
dotnet_naming_rule.constants_rule.severity = warning
8282
dotnet_naming_rule.constants_rule.style = upper_camel_case_style
8383
dotnet_naming_rule.constants_rule.symbols = constants_symbols
84-
dotnet_naming_rule.constant_fields_should_be_pascal_case_rule.import_to_resharper = True
84+
dotnet_naming_rule.constant_fields_should_be_pascal_case_rule.import_to_resharper = true
8585
dotnet_naming_rule.constant_fields_should_be_pascal_case_rule.resharper_description = constant_fields_should_be_pascal_case
8686
dotnet_naming_rule.constant_fields_should_be_pascal_case_rule.severity = suggestion
8787
dotnet_naming_rule.constant_fields_should_be_pascal_case_rule.style = upper_camel_case_style

c#/Directory.Build.props

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<PathMap>$(MSBuildProjectDirectory)=/</PathMap>
99
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
11+
<AnalysisMode>Recommended</AnalysisMode>
12+
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
1113
</PropertyGroup>
1214
<ItemGroup>
1315
<AdditionalFiles Include="$(MSBuildThisFileDirectory)\stylecop.json" />

c#/GlobalSuppressions.cs

+11
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@
4343
[assembly: SuppressMessage("Style", "CC0001:You should use 'var' whenever possible.")]
4444
[assembly: SuppressMessage("Style", "CC0037:Remove commented code.")]
4545
[assembly: SuppressMessage("Documentation", "AV2305:Missing XML comment for internally visible type, member or parameter")]
46+
[assembly: SuppressMessage("Style", "CC0061:Asynchronous method can be terminated with the 'Async' keyword.")]
47+
[assembly: SuppressMessage("Style", "MA0003:Add parameter name to improve readability")]
48+
[assembly: SuppressMessage("Style", "MA0007:Add a comma after the last value")]
49+
[assembly: SuppressMessage("Style", "VSTHRD200:Use \"Async\" suffix for async methods")]
50+
[assembly: SuppressMessage("Usage", "MA0004:Use Task.ConfigureAwait")]
51+
[assembly: SuppressMessage("Usage", "MA0006:Use String.Equals instead of equality operator")]
52+
[assembly: SuppressMessage("Usage", "MA0015:Specify the parameter name in ArgumentException")]
53+
[assembly: SuppressMessage("Performance", "CA1848:Use the LoggerMessage delegates")]
54+
[assembly: SuppressMessage("Usage", "CC0057:Unused parameters")]
55+
[assembly: SuppressMessage("Naming", "CA1711:Identifiers should not have incorrect suffix")]
56+
[assembly: SuppressMessage("Naming", "CA1716:Identifiers should not match keywords")]
4657

4758
[assembly: SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1101:Prefix local calls with this")]
4859
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:Elements should be documented")]

c#/crawler/src/Db/ProtoBufRepeatedFieldJsonConverter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class ProtoBufRepeatedFieldJsonConverter<TProtoBuf>
55
{
66
public override RepeatedField<TProtoBuf> Read
77
(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
8-
throw new NotImplementedException();
8+
throw new NotSupportedException();
99

1010
public override void Write
1111
(Utf8JsonWriter writer, RepeatedField<TProtoBuf> value, JsonSerializerOptions options) =>

c#/crawler/src/Db/Revision/RevisionWithSplitting.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public abstract class RevisionWithSplitting<TBaseRevision> : IRevision
77
public ushort? NullFieldsBitMask { get; set; }
88
public Dictionary<Type, TBaseRevision> SplitEntities { get; } = [];
99

10-
public virtual bool IsAllFieldsIsNullExceptSplit() => throw new NotImplementedException();
10+
public virtual bool IsAllFieldsIsNullExceptSplit() => throw new NotSupportedException();
1111

1212
protected TValue? GetSplitEntityValue<TSplitEntity, TValue>
1313
(Func<TSplitEntity, TValue?> valueSelector)

c#/crawler/src/Tieba/Crawl/Saver/BaseSaver.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public abstract class BaseSaver<TPost, TBaseRevision>(
1515
protected event PostSaveEventHandler PostSaveEvent = () => { };
1616

1717
public virtual FieldChangeIgnoranceDelegates UserFieldChangeIgnorance =>
18-
throw new NotImplementedException();
18+
throw new NotSupportedException();
1919
public string PostType { get; } = postType;
2020
protected ConcurrentDictionary<ulong, TPost> Posts { get; } = posts;
2121
protected AuthorRevisionSaver AuthorRevisionSaver { get; } = authorRevisionSaverFactory(postType);

c#/crawler/src/Tieba/Crawl/Saver/CommonInSavers.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public abstract class CommonInSavers<TBaseRevision>(ILogger<CommonInSavers<TBase
99
protected delegate void RevisionUpsertDelegate(CrawlerDbContext db, IEnumerable<TBaseRevision> revision);
1010

1111
protected virtual Dictionary<Type, RevisionUpsertDelegate>
12-
RevisionUpsertDelegatesKeyBySplitEntityType => throw new NotImplementedException();
12+
RevisionUpsertDelegatesKeyBySplitEntityType => throw new NotSupportedException();
1313

1414
protected void SavePostsOrUsers<TPostOrUser, TRevision>(
1515
CrawlerDbContext db,
@@ -109,7 +109,7 @@ bool IsTimestampingFieldName(string name) => name is nameof(IPost.LastSeenAt)
109109
.ForEach(g => RevisionUpsertDelegatesKeyBySplitEntityType[g.Key](db, g));
110110
}
111111

112-
protected virtual NullFieldsBitMask GetRevisionNullFieldBitMask(string fieldName) => throw new NotImplementedException();
112+
protected virtual NullFieldsBitMask GetRevisionNullFieldBitMask(string fieldName) => throw new NotSupportedException();
113113

114114
private static bool IsLatestReplierUser(string pName, PropertyEntry p, EntityEntry entry)
115115
{

c#/crawler/src/Tieba/Crawl/UserParserAndSaver.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void ParseUsers(IEnumerable<TbClient.User> users) =>
4545
{
4646
static (string Portrait, uint? UpdateTime) ExtractPortrait(string portrait) =>
4747
ExtractPortraitRegex().Match(portrait) is {Success: true} m
48-
? (m.Groups[1].Value, Time.Parse(m.Groups[2].ValueSpan))
48+
? (m.Groups["portrait"].Value, Time.Parse(m.Groups["timestamp"].ValueSpan))
4949
: (portrait, null);
5050

5151
var uid = el.Uid;
@@ -132,6 +132,6 @@ public void PostSaveHook()
132132
lock (UserIdLocks) if (_savedUsersId.Count != 0) UserIdLocks.ExceptWith(_savedUsersId);
133133
}
134134

135-
[GeneratedRegex("^(.+)\\?t=([0-9]+)$", RegexOptions.Compiled, matchTimeoutMilliseconds: 100)]
135+
[GeneratedRegex("^(?<portrait>.+)\\?t=(?<timestamp>[0-9]+)$", RegexOptions.Compiled, matchTimeoutMilliseconds: 100)]
136136
private static partial Regex ExtractPortraitRegex();
137137
}

c#/crawler/src/Worker/ArchiveCrawlWorker.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class ArchiveCrawlWorker(
2323
private readonly string _forumName = "";
2424

2525
// ReSharper disable once ConvertToConstant.Local
26-
private readonly Fid _fid = 0;
26+
private readonly Fid _fid = 1;
2727

2828
// https://en.wikipedia.org/wiki/Moving_average#Cumulative_average
2929
public static float GetCumulativeAverage(float currentCa, float previousCa, int currentIndex) =>

c#/crawler/src/Worker/ForumModeratorRevisionCrawlWorker.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ from rev in db.ForumModeratorRevisions.AsNoTracking()
8888
}).Where(e => e.Rank == 1)
8989
.ToLinqToDB().ToList();
9090

91-
db.ForumModeratorRevisions.AddRange(revisions.ExceptBy(
91+
await db.ForumModeratorRevisions.AddRangeAsync(revisions.ExceptBy(
9292
existingLatestRevisions.Select(e => (e.Portrait, e.ModeratorTypes)),
93-
rev => (rev.Portrait, rev.ModeratorTypes)));
94-
db.ForumModeratorRevisions.AddRange(existingLatestRevisions
93+
rev => (rev.Portrait, rev.ModeratorTypes)), stoppingToken);
94+
await db.ForumModeratorRevisions.AddRangeAsync(existingLatestRevisions
9595

9696
// filter out revisions that recorded someone who resigned from moderators
9797
.Where(e => e.ModeratorTypes != "")
@@ -102,7 +102,7 @@ from rev in db.ForumModeratorRevisions.AsNoTracking()
102102
Fid = fid,
103103
Portrait = e.Portrait,
104104
ModeratorTypes = "" // moderator only exists in DB means the user is no longer a moderator
105-
}));
105+
}), stoppingToken);
106106

107107
_ = await db.SaveChangesAsync(stoppingToken);
108108
await transaction.CommitAsync(stoppingToken);

c#/imagePipeline/src/Consumer/HashConsumer.cs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public sealed class HashConsumer : MatrixConsumer, IDisposable
88
private readonly FailedImageHandler _failedImageHandler;
99
private readonly Dictionary<ImgHashBase, Action<ImageHash, byte[]>> _imageHashSettersKeyByAlgorithm;
1010

11+
[SuppressMessage("Correctness", "SS004:Implement Equals() and GetHashcode() methods for a type used in a collection.")]
1112
public HashConsumer(FailedImageHandler failedImageHandler)
1213
{
1314
_failedImageHandler = failedImageHandler;

c#/imagePipeline/src/Consumer/IConsumer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ public interface IConsumer<in T>
44
{
55
public (IEnumerable<ImageId> Failed, IEnumerable<ImageId> Consumed) Consume(
66
ImagePipelineDbContext db,
7-
IReadOnlyCollection<T> imageKeysWithMatrix,
7+
IReadOnlyCollection<T> imageKeysWithT,
88
CancellationToken stoppingToken = default);
99
}

c#/imagePipeline/src/Consumer/MatrixConsumer.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ public abstract class MatrixConsumer : IConsumer<ImageKeyWithMatrix>
44
{
55
public (IEnumerable<ImageId> Failed, IEnumerable<ImageId> Consumed) Consume(
66
ImagePipelineDbContext db,
7-
IReadOnlyCollection<ImageKeyWithMatrix> imageKeysWithMatrix,
7+
IReadOnlyCollection<ImageKeyWithMatrix> imageKeysWithT,
88
CancellationToken stoppingToken = default)
99
{
1010
// defensive clone to prevent any consumer mutate the original matrix given in param
1111
var clonedImageKeysWithMatrix =
12-
imageKeysWithMatrix.Select(i => i with {Matrix = i.Matrix.Clone()}).ToList();
12+
imageKeysWithT.Select(i => i with {Matrix = i.Matrix.Clone()}).ToList();
1313
try
1414
{
1515
var failed = ConsumeInternal(db, clonedImageKeysWithMatrix, stoppingToken).ToList();

c#/imagePipeline/src/Consumer/MetadataConsumer.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ ulong[] GetCommonXxHash3ToIgnore(string key) =>
4040

4141
public (IEnumerable<ImageId> Failed, IEnumerable<ImageId> Consumed) Consume(
4242
ImagePipelineDbContext db,
43-
IReadOnlyCollection<ImageWithBytes> imagesWithBytes,
43+
IReadOnlyCollection<ImageWithBytes> imageKeysWithT,
4444
CancellationToken stoppingToken = default)
4545
{
4646
var metadataEithers = _failedImageHandler
47-
.TrySelect(imagesWithBytes,
47+
.TrySelect(imageKeysWithT,
4848
imageWithBytes => imageWithBytes.ImageInReply.ImageId,
4949
GetImageMetaData(stoppingToken))
5050
.ToList();
5151
db.ImageMetadata.AddRange(metadataEithers.Rights());
5252
var failed = metadataEithers.Lefts().ToList();
53-
return (failed, imagesWithBytes.Select(i => i.ImageInReply.ImageId).Except(failed));
53+
return (failed, imageKeysWithT.Select(i => i.ImageInReply.ImageId).Except(failed));
5454
}
5555

5656
private Func<ImageWithBytes, ImageMetadata> GetImageMetaData
@@ -299,7 +299,7 @@ private static partial class ExifDateTimeTagValuesParser
299299

300300
[GeneratedRegex(
301301
"^.*(?<dateTime>[0-9]{4}:[0-9]{2}:[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}(?!(上|下)午)?).*$",
302-
RegexOptions.Compiled, matchTimeoutMilliseconds: 100)]
302+
RegexOptions.Compiled | RegexOptions.ExplicitCapture, matchTimeoutMilliseconds: 100)]
303303
private static partial Regex ExtractCommonExifDateTimeWithLeadingOrTrailingCharsRegex();
304304

305305
[SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1025:Code should not contain multiple whitespace in a row")]

c#/imagePipeline/src/ImageBatchConsumingWorker.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ void MarkImagesInReplyAsConsumed
7373
.ForEach(entry => entry.Property(selector).CurrentValue = true);
7474

7575
logger.LogTrace("Start to consume {} image(s): [{}]",
76-
imagesWithBytes.Count, string.Join(",", imagesInReply.Select(i => i.ImageId)));
76+
imagesWithBytes.Count, string.Join(',', imagesInReply.Select(i => i.ImageId)));
7777
var sw = new Stopwatch();
7878
void LogStopwatch(string consumerType, IReadOnlyCollection<ImageId> imagesId) =>
7979
logger.LogTrace("Spend {}ms to {} for {} image(s): [{}]",
80-
sw.ElapsedMilliseconds, consumerType, imagesId.Count, string.Join(",", imagesId));
80+
sw.ElapsedMilliseconds, consumerType, imagesId.Count, string.Join(',', imagesId));
8181

8282
void ConsumeConsumer<TImage, TConsumer>(
8383
Expression<Func<ImageInReply, bool>> selector, IReadOnlyCollection<TImage> images,
@@ -96,7 +96,7 @@ void ConsumeConsumer<TImage, TConsumer>(
9696

9797
if (failed.Count == 0) return;
9898
logger.LogError("Failed to {} for {} image(s): [{}]",
99-
consumerType, failed.Count, string.Join(",", failed));
99+
consumerType, failed.Count, string.Join(',', failed));
100100
}
101101

102102
ConsumeConsumer(i => i.MetadataConsumed,
@@ -243,9 +243,9 @@ where imageKeysWithMatrix
243243
.ExceptBy(recognizedTextLines.Select(i => i.ImageId), i => i.ImageId).ToList();
244244

245245
// insert their previously recognized lines into the table of current forum and script
246-
db.ImageOcrLines.AddRange(recognizedTextLines.IntersectBy(
246+
await db.ImageOcrLines.AddRangeAsync(recognizedTextLines.IntersectBy(
247247
imagesInCurrentFid.Except(uniqueImagesInCurrentFid).Select(i => i.ImageId),
248-
i => i.ImageId));
248+
i => i.ImageId), stoppingToken);
249249
recognizedTextLines.AddRange(await ConsumeByFidAndScript(db, fid, script, uniqueImagesInCurrentFid));
250250
_ = await db.SaveChangesAsync(stoppingToken);
251251
}
@@ -272,10 +272,10 @@ async Task<IEnumerable<ImageOcrLine>> ConsumeByFidAndScript(
272272
var failed = imagesId.Failed.ToList();
273273
if (failed.Count != 0)
274274
logger.LogError("Failed to detect and recognize {} script text for fid {} in {} image(s): [{}]",
275-
script, fid, failed.Count, string.Join(",", failed));
275+
script, fid, failed.Count, string.Join(',', failed));
276276
logger.LogTrace("Spend {}ms to detect and recognize {} script text for fid {} in {} image(s): [{}]",
277277
sw.ElapsedMilliseconds, script, fid, imagesInCurrentFid.Count,
278-
string.Join(",", imagesInCurrentFid.Select(i => i.ImageId)));
278+
string.Join(',', imagesInCurrentFid.Select(i => i.ImageId)));
279279

280280
return ocrConsumer.RecognizedTextLines;
281281
}

c#/shared/src/TbmDbContext.cs

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public class TbmDbContext<TModelCacheKeyFactory> : TbmDbContext
6767
public DbSet<ImageInReply> ImageInReplies => Set<ImageInReply>();
6868
public DbSet<ReplyContentImage> ReplyContentImages => Set<ReplyContentImage>();
6969

70+
[SuppressMessage("Naming", "CA1725:Parameter names should match base declaration")]
7071
[SuppressMessage("Critical Code Smell", "S927:Parameter names should match base declaration and other partial definitions")]
7172
[SuppressMessage("Style", "IDE0058:Expression value is never used")]
7273
protected override void OnConfiguring(DbContextOptionsBuilder options)
@@ -88,6 +89,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder options)
8889
if (dbSettings.GetValue("EnableSensitiveDataLogging", false)) options.EnableSensitiveDataLogging();
8990
}
9091

92+
[SuppressMessage("Naming", "CA1725:Parameter names should match base declaration")]
9193
[SuppressMessage("Critical Code Smell", "S927:Parameter names should match base declaration and other partial definitions")]
9294
[SuppressMessage("Style", "IDE0058:Expression value is never used")]
9395
protected override void OnModelCreating(ModelBuilder b)

0 commit comments

Comments
 (0)