Skip to content

Commit ce39dd4

Browse files
Add readonly to struct methods (#8656)
In recent dogfood VS builds we've gotten many IDE0251 suggestions, some warnings escalated to errors in StringTools. Follow many of those suggestions, but bump the rule down to suggestion always, like the related IDE0250.
1 parent 457218b commit ce39dd4

23 files changed

+52
-49
lines changed

.editorconfig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,11 @@ dotnet_diagnostic.IDE0241.severity = suggestion
390390
# Struct can be made 'readonly'
391391
dotnet_diagnostic.IDE0250.severity = suggestion
392392

393+
# Struct methods can be made 'readonly'
394+
dotnet_diagnostic.IDE0251.severity = suggestion
395+
393396
# Null check can be simplified
394397
dotnet_diagnostic.IDE0270.severity = suggestion
395398

396399
# naming rule violation
397-
dotnet_diagnostic.IDE1006.severity = suggestion
400+
dotnet_diagnostic.IDE1006.severity = suggestion

src/Build/Collections/ConvertingEnumerable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ internal ConvertingEnumerator(IEnumerator<TFrom2> backingEnumerator, Func<TFrom2
8282
/// <summary>
8383
/// Get the current element, converted
8484
/// </summary>
85-
public TTo2 Current
85+
public readonly TTo2 Current
8686
{
8787
get
8888
{

src/Build/Construction/ProjectElementContainer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ internal ProjectElementSiblingEnumerable(ProjectElement initial, bool forwards =
742742
/// <summary>
743743
/// Get enumerator
744744
/// </summary>
745-
public IEnumerator<ProjectElement> GetEnumerator()
745+
public readonly IEnumerator<ProjectElement> GetEnumerator()
746746
{
747747
return _enumerator;
748748
}
@@ -808,7 +808,7 @@ object System.Collections.IEnumerator.Current
808808
/// <summary>
809809
/// Dispose. Do nothing.
810810
/// </summary>
811-
public void Dispose()
811+
public readonly void Dispose()
812812
{
813813
}
814814

src/Build/Definition/Project.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2558,7 +2558,7 @@ public static CumulativeRemoveElementData Create()
25582558
};
25592559
}
25602560

2561-
public void AccumulateInformationFromRemoveItemSpec(EvaluationItemSpec removeSpec)
2561+
public readonly void AccumulateInformationFromRemoveItemSpec(EvaluationItemSpec removeSpec)
25622562
{
25632563
IEnumerable<string> removeSpecFragmentStrings = removeSpec.FlattenFragmentsAsStrings();
25642564
var removeGlob = removeSpec.ToMSBuildGlob();

src/Build/Definition/ToolsetReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ private MSBuildExtensionsPathReferenceKind(string value)
817817
/// <summary>
818818
/// Returns the corresponding property name - eg. "$(MSBuildExtensionsPath32)"
819819
/// </summary>
820-
public string MSBuildPropertyName => String.Format($"$({StringRepresentation})");
820+
public readonly string MSBuildPropertyName => String.Format($"$({StringRepresentation})");
821821

822822
/// <summary>
823823
/// Tries to find a reference to MSBuildExtensionsPath* property in the given string

src/Build/Evaluation/Expander.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public void Add(ReadOnlyMemory<char> span)
210210
/// concatenation of the string representation of the values, each additionally subjected
211211
/// to file path adjustment.
212212
/// </returns>
213-
public object GetResult()
213+
public readonly object GetResult()
214214
{
215215
CheckDisposed();
216216
if (_firstObject != null)
@@ -235,7 +235,7 @@ public void Dispose()
235235
/// <summary>
236236
/// Throws <see cref="ObjectDisposedException"/> if this concatenator is already disposed.
237237
/// </summary>
238-
private void CheckDisposed() =>
238+
private readonly void CheckDisposed() =>
239239
ErrorUtilities.VerifyThrowObjectDisposed(!_disposed, nameof(SpanBasedConcatenator));
240240

241241
/// <summary>
@@ -3142,7 +3142,7 @@ private struct FunctionBuilder<T>
31423142
/// </summary>
31433143
public UsedUninitializedProperties UsedUninitializedProperties { get; set; }
31443144

3145-
internal Function<T> Build()
3145+
internal readonly Function<T> Build()
31463146
{
31473147
return new Function<T>(
31483148
ReceiverType,

src/Build/Evaluation/ItemsAndMetadataPair.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal ItemsAndMetadataPair(HashSet<string> items, Dictionary<string, Metadata
4040
/// </summary>
4141
internal HashSet<string> Items
4242
{
43-
get
43+
readonly get
4444
{
4545
return _items;
4646
}
@@ -58,7 +58,7 @@ internal HashSet<string> Items
5858
/// </summary>
5959
internal Dictionary<string, MetadataReference> Metadata
6060
{
61-
get
61+
readonly get
6262
{
6363
return _metadata;
6464
}

src/Build/Evaluation/LazyItemEvaluator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public ItemData(I item, ProjectItemElement originatingItemElement, int elementOr
135135
_normalizedItemValue = normalizedItemValue;
136136
}
137137

138-
public ItemData Clone(IItemFactory<I, I> itemFactory, ProjectItemElement initialItemElementForFactory)
138+
public readonly ItemData Clone(IItemFactory<I, I> itemFactory, ProjectItemElement initialItemElementForFactory)
139139
{
140140
// setting the factory's item element to the original item element that produced the item
141141
// otherwise you get weird things like items that appear to have been produced by update elements

src/Build/Graph/ProjectGraph.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ public ProjectGraphBuildRequest(ProjectGraphNode node, ImmutableList<string> tar
766766

767767
public ImmutableList<string> RequestedTargets { get; }
768768

769-
public bool Equals(ProjectGraphBuildRequest other)
769+
public readonly bool Equals(ProjectGraphBuildRequest other)
770770
{
771771
if (Node != other.Node
772772
|| RequestedTargets.Count != other.RequestedTargets.Count)
@@ -786,12 +786,12 @@ public bool Equals(ProjectGraphBuildRequest other)
786786
return true;
787787
}
788788

789-
public override bool Equals(object obj)
789+
public override readonly bool Equals(object obj)
790790
{
791791
return !(obj is null) && obj is ProjectGraphBuildRequest graphNodeWithTargets && Equals(graphNodeWithTargets);
792792
}
793793

794-
public override int GetHashCode()
794+
public override readonly int GetHashCode()
795795
{
796796
unchecked
797797
{

src/Build/Graph/ProjectGraphEntryPoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ internal static IEnumerable<ProjectGraphEntryPoint> CreateEnumerable(IEnumerable
6161
}
6262
}
6363

64-
internal IEnumerable<ProjectGraphEntryPoint> AsEnumerable()
64+
internal readonly IEnumerable<ProjectGraphEntryPoint> AsEnumerable()
6565
{
6666
yield return this;
6767
}

0 commit comments

Comments
 (0)