Skip to content

Commit 51d2946

Browse files
authored
Code cleanup: Phase 1 (#1785)
Tidying up for newer C# versions and removing old cruft: - Removing unneeded pragmas (e.g. transitive obsoletes) - Switch expressions - Expression body usages, where it's cleaner with new switch expressions - Statics where useful to remove virtual calls - A handful of pattern matching usages where we were repeat casting - `StringBuilder` optimizations for .NET Core (single char appends) - Suppression fixes for contention between frameworks - Suppression additions for dupe enum values (that are there for compat) - Fix messages/arguments for many argument exceptions - Re-enables the GC test (was the only skipped one - no longer needed) - Misc compiler warning fixes - Fixes KestrelRedisServer's conflict with `System.Runtime.CompilerServices.Unsafe` These are broken up by commit for an easier review, but didn't want to make 12 PRs for some maintenance catch-up. Note: this does not fix all informational message, including a few suspects for actual issues and some that are the way we structure `Dispose() => Dispose(true)` in a few files. Given those may change functionality, will do a follow-up PR there. This is meant to get all the noise gone and general cleanup without any significant impact anywhere.
1 parent 5324b15 commit 51d2946

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+439
-575
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ dotnet_style_explicit_tuple_names = true:suggestion
4848
# Ignore silly if statements
4949
dotnet_style_prefer_conditional_expression_over_return = false:none
5050

51+
# Don't warn on things that actually need suppressing
52+
dotnet_remove_unnecessary_suppression_exclusions = CA1009,CA1063,CA1069,CA1416,CA1816,CA1822,CA2202,CS0618,IDE0060,IDE0062,RCS1047,RCS1085,RCS1090,RCS1194,RCS1231
53+
5154
# CSharp code style settings:
5255
[*.cs]
5356
# Prefer method-like constructs to have a expression-body
@@ -66,6 +69,11 @@ csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
6669
csharp_style_inlined_variable_declaration = true:suggestion
6770
csharp_style_throw_expression = true:suggestion
6871
csharp_style_conditional_delegate_call = true:suggestion
72+
csharp_prefer_simple_using_statement = true:silent
73+
74+
# Disable range operator suggestions
75+
csharp_style_prefer_range_operator = false:none
76+
csharp_style_prefer_index_operator = false:none
6977

7078
# Newline settings
7179
csharp_new_line_before_open_brace = all

src/NRediSearch/Aggregation/AggregationRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public AggregationRequest GroupBy(IList<string> fields, IList<Reducer> reducers)
9494
return this;
9595
}
9696

97-
public AggregationRequest GroupBy(String field, params Reducer[] reducers)
97+
public AggregationRequest GroupBy(string field, params Reducer[] reducers)
9898
{
9999
return GroupBy(new string[] { field }, reducers);
100100
}

src/NRediSearch/Client.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public sealed class ConfiguredIndexOptions
134134
public static IndexOptions Default => new IndexOptions();
135135

136136
private IndexOptions _options;
137-
private IndexDefinition _definition;
137+
private readonly IndexDefinition _definition;
138138
private string[] _stopwords;
139139

140140
public ConfiguredIndexOptions(IndexOptions options = IndexOptions.Default)
@@ -1276,7 +1276,7 @@ public async Task<Document[]> GetDocumentsAsync(params string[] docIds)
12761276
{
12771277
if (docIds.Length == 0)
12781278
{
1279-
return new Document[] { };
1279+
return Array.Empty<Document>();
12801280
}
12811281

12821282
var args = new List<object>

src/NRediSearch/Extensions.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@ internal static string AsRedisString(this double value, bool forceDecimal = fals
2323
return value.ToString(forceDecimal ? "#.0" : "G17", NumberFormatInfo.InvariantInfo);
2424
}
2525
}
26-
internal static string AsRedisString(this GeoUnit value)
26+
internal static string AsRedisString(this GeoUnit value) => value switch
2727
{
28-
switch (value)
29-
{
30-
case GeoUnit.Feet: return "ft";
31-
case GeoUnit.Kilometers: return "km";
32-
case GeoUnit.Meters: return "m";
33-
case GeoUnit.Miles: return "mi";
34-
default: throw new InvalidOperationException($"Unknown unit: {value}");
35-
}
36-
}
28+
GeoUnit.Feet => "ft",
29+
GeoUnit.Kilometers => "km",
30+
GeoUnit.Meters => "m",
31+
GeoUnit.Miles => "mi",
32+
_ => throw new InvalidOperationException($"Unknown unit: {value}"),
33+
};
3734
}
3835
}

src/NRediSearch/QueryBuilder/GeoValue.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public GeoValue(double lon, double lat, double radius, GeoUnit unit)
2121
public override string ToString()
2222
{
2323
return new StringBuilder("[")
24-
.Append(_lon.AsRedisString(true)).Append(" ")
25-
.Append(_lat.AsRedisString(true)).Append(" ")
26-
.Append(_radius.AsRedisString(true)).Append(" ")
24+
.Append(_lon.AsRedisString(true)).Append(' ')
25+
.Append(_lat.AsRedisString(true)).Append(' ')
26+
.Append(_radius.AsRedisString(true)).Append(' ')
2727
.Append(_unit.AsRedisString())
28-
.Append("]").ToString();
28+
.Append(']').ToString();
2929
}
3030

3131
public override bool IsCombinable() => false;

src/NRediSearch/QueryBuilder/QueryNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public virtual string ToString(ParenMode mode)
8080

8181
if (ShouldUseParens(mode))
8282
{
83-
sb.Append("(");
83+
sb.Append('(');
8484
}
8585
var sj = new StringJoiner(sb, GetJoinString());
8686
foreach (var n in children)
@@ -89,7 +89,7 @@ public virtual string ToString(ParenMode mode)
8989
}
9090
if (ShouldUseParens(mode))
9191
{
92-
sb.Append(")");
92+
sb.Append(')');
9393
}
9494
return sb.ToString();
9595
}

src/NRediSearch/QueryBuilder/RangeValue.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ private static void AppendNum(StringBuilder sb, double n, bool inclusive)
1515
{
1616
if (!inclusive)
1717
{
18-
sb.Append("(");
18+
sb.Append('(');
1919
}
2020
sb.Append(n.AsRedisString(true));
2121
}
2222

2323
public override string ToString()
2424
{
2525
StringBuilder sb = new StringBuilder();
26-
sb.Append("[");
26+
sb.Append('[');
2727
AppendNum(sb, from, inclusiveMin);
28-
sb.Append(" ");
28+
sb.Append(' ');
2929
AppendNum(sb, to, inclusiveMax);
30-
sb.Append("]");
30+
sb.Append(']');
3131
return sb.ToString();
3232
}
3333

src/NRediSearch/QueryBuilder/ValueNode.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private string ToStringCombinable(ParenMode mode)
4040
StringBuilder sb = new StringBuilder(FormatField());
4141
if (_values.Length > 1 || mode == ParenMode.Always)
4242
{
43-
sb.Append("(");
43+
sb.Append('(');
4444
}
4545
var sj = new StringJoiner(sb, _joinString);
4646
foreach (var v in _values)
@@ -49,7 +49,7 @@ private string ToStringCombinable(ParenMode mode)
4949
}
5050
if (_values.Length > 1 || mode == ParenMode.Always)
5151
{
52-
sb.Append(")");
52+
sb.Append(')');
5353
}
5454
return sb.ToString();
5555
}
@@ -64,7 +64,7 @@ private string ToStringDefault(ParenMode mode)
6464
var sb = new StringBuilder();
6565
if (useParen)
6666
{
67-
sb.Append("(");
67+
sb.Append('(');
6868
}
6969
var sj = new StringJoiner(sb, _joinString);
7070
foreach (var v in _values)
@@ -73,7 +73,7 @@ private string ToStringDefault(ParenMode mode)
7373
}
7474
if (useParen)
7575
{
76-
sb.Append(")");
76+
sb.Append(')');
7777
}
7878
return sb.ToString();
7979
}

src/NRediSearch/Schema.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,14 @@ internal Field(string name, FieldType type, bool sortable, bool noIndex = false)
3636

3737
internal virtual void SerializeRedisArgs(List<object> args)
3838
{
39-
static object GetForRedis(FieldType type)
39+
static object GetForRedis(FieldType type) => type switch
4040
{
41-
switch (type)
42-
{
43-
case FieldType.FullText: return "TEXT".Literal();
44-
case FieldType.Geo: return "GEO".Literal();
45-
case FieldType.Numeric: return "NUMERIC".Literal();
46-
case FieldType.Tag: return "TAG".Literal();
47-
default: throw new ArgumentOutOfRangeException(nameof(type));
48-
}
49-
}
41+
FieldType.FullText => "TEXT".Literal(),
42+
FieldType.Geo => "GEO".Literal(),
43+
FieldType.Numeric => "NUMERIC".Literal(),
44+
FieldType.Tag => "TAG".Literal(),
45+
_ => throw new ArgumentOutOfRangeException(nameof(type)),
46+
};
5047
args.Add(Name);
5148
args.Add(GetForRedis(Type));
5249
if (Sortable) { args.Add("SORTABLE".Literal()); }

src/NRediSearch/Suggestion.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public SuggestionBuilder Payload(string payload)
9191

9292
public Suggestion Build() => Build(false);
9393

94+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0071:Simplify interpolation", Justification = "Allocations (string.Concat vs. string.Format)")]
9495
internal Suggestion Build(bool fromServer)
9596
{
9697
bool isStringMissing = _string == null;

0 commit comments

Comments
 (0)