Skip to content

Commit

Permalink
Merge pull request #4846 from peppy/var-inspections-update
Browse files Browse the repository at this point in the history
Update `var` usage inspections to disallow for built-in types
  • Loading branch information
smoogipoo authored Oct 27, 2021
2 parents 2ecda2d + df5a006 commit 7a743d3
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ dotnet_style_qualification_for_event = false:warning
dotnet_style_predefined_type_for_locals_parameters_members = true:warning
dotnet_style_predefined_type_for_member_access = true:warning
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_for_built_in_types = true:none
csharp_style_var_for_built_in_types = false:warning
csharp_style_var_elsewhere = true:silent

#Style - modifiers
Expand Down
5 changes: 4 additions & 1 deletion osu-framework.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=StringLiteralTypo/@EntryIndexedValue">HINT</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=StringStartsWithIsCultureSpecific/@EntryIndexedValue">WARNING</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=StructCanBeMadeReadOnly/@EntryIndexedValue">WARNING</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=SuggestVarOrType_005FBuiltInTypes/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=SuggestVarOrType_005FBuiltInTypes/@EntryIndexedValue">SUGGESTION</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=SuggestVarOrType_005FSimpleTypes/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=SuspiciousTypeConversion_002EGlobal/@EntryIndexedValue">HINT</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=SwitchStatementMissingSomeCases/@EntryIndexedValue">DO_NOT_SHOW</s:String>
Expand Down Expand Up @@ -301,6 +301,9 @@
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_BEFORE_BINARY_OPSIGN/@EntryValue">True</s:Boolean>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LIMIT/@EntryValue">200</s:Int64>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_OBJECT_AND_COLLECTION_INITIALIZER_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForBuiltInTypes/@EntryValue">UseExplicitType</s:String>
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForOtherTypes/@EntryValue">UseVarWhenEvident</s:String>
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForSimpleTypes/@EntryValue">UseVarWhenEvident</s:String>
<s:Boolean x:Key="/Default/CodeStyle/EncapsulateField/MakeFieldPrivate/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/EncapsulateField/UseAutoProperty/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=AABB/@EntryIndexedValue">AABB</s:String>
Expand Down
4 changes: 2 additions & 2 deletions osu.Framework.Tests/IO/TestWebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static TestWebRequest()
[Test, Retry(5)]
public void TestValidGet([ValueSource(nameof(protocols))] string protocol, [Values(true, false)] bool async)
{
var url = $"{protocol}://{host}/get";
string url = $"{protocol}://{host}/get";
var request = new JsonWebRequest<HttpBinGetResponse>(url)
{
Method = HttpMethod.Get,
Expand All @@ -62,7 +62,7 @@ public void TestValidGet([ValueSource(nameof(protocols))] string protocol, [Valu
[Test, Retry(5)]
public void TestCustomUserAgent([ValueSource(nameof(protocols))] string protocol, [Values(true, false)] bool async)
{
var url = $"{protocol}://{host}/get";
string url = $"{protocol}://{host}/get";
var request = new CustomUserAgentWebRequest(url)
{
Method = HttpMethod.Get,
Expand Down
4 changes: 2 additions & 2 deletions osu.Framework/Graphics/Containers/SearchContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ protected override void Update()

private void performFilter()
{
var terms = (searchTerm ?? string.Empty).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string[] terms = (searchTerm ?? string.Empty).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
Children.OfType<IFilterable>().ForEach(child => match(child, terms, terms.Length > 0, allowNonContiguousMatching));
}

private static bool match(IFilterable filterable, IEnumerable<string> searchTerms, bool searchActive, bool nonContiguousMatching)
{
//Words matched by parent is not needed to match children
var childTerms = searchTerms.Where(term =>
string[] childTerms = searchTerms.Where(term =>
!filterable.FilterTerms.Any(filterTerm =>
checkTerm(filterTerm, term, nonContiguousMatching))).ToArray();

Expand Down
4 changes: 2 additions & 2 deletions osu.Framework/Input/Handlers/Midi/MidiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private void closeDevice(IMidiInput device)
private void onMidiMessageReceived(object sender, MidiReceivedEventArgs e)
{
Debug.Assert(sender is IMidiInput);
var senderId = ((IMidiInput)sender).Details.Id;
string senderId = ((IMidiInput)sender).Details.Id;

try
{
Expand All @@ -133,7 +133,7 @@ private void onMidiMessageReceived(object sender, MidiReceivedEventArgs e)
}
catch (Exception exception)
{
var dataString = string.Join("-", e.Data.Select(b => b.ToString("X2")));
string dataString = string.Join("-", e.Data.Select(b => b.ToString("X2")));
Logger.Error(exception, $"An exception occurred while reading MIDI data from sender {senderId}: {dataString}");
}
}
Expand Down
8 changes: 4 additions & 4 deletions osu.Framework/Testing/RoslynTypeReferenceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ private void getReferencedFilesRecursive(DirectedTypeNode node, HashSet<string>

if (expansions.Count > 1)
{
var q1 = getMedian(expansions.Take(expansions.Count / 2).ToList(), out var q1Centre);
var q3 = getMedian(expansions.Skip((int)Math.Ceiling(expansions.Count / 2f)).ToList(), out _);
ulong q1 = getMedian(expansions.Take(expansions.Count / 2).ToList(), out int q1Centre);
ulong q3 = getMedian(expansions.Skip((int)Math.Ceiling(expansions.Count / 2f)).ToList(), out _);

rightBound = q3 + 3 * (q3 - q1);

Expand Down Expand Up @@ -554,7 +554,7 @@ private ulong getMedian(List<ulong> range, out int centre)

private bool typeInheritsFromGame(TypeReference reference)
{
if (typeInheritsFromGameCache.TryGetValue(reference, out var existing))
if (typeInheritsFromGameCache.TryGetValue(reference, out bool existing))
return existing;

// When used via a nuget package, the local type name seems to always be more qualified than the symbol's type name.
Expand Down Expand Up @@ -624,7 +624,7 @@ private async Task<SemanticModel> getSemanticModelAsync(SyntaxTree syntaxTree)
/// <returns>The <see cref="Project"/> containing the currently-executing test.</returns>
private Project findTestProject()
{
var executingAssembly = Assembly.GetEntryAssembly()?.GetName().Name;
string executingAssembly = Assembly.GetEntryAssembly()?.GetName().Name;
return solution.Projects.FirstOrDefault(p => p.AssemblyName == executingAssembly);
}

Expand Down
14 changes: 7 additions & 7 deletions osu.Framework/Testing/TestBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ protected override void LoadComplete()

if (CurrentTest == null)
{
var lastTest = config.Get<string>(TestBrowserSetting.LastTest);
string lastTest = config.Get<string>(TestBrowserSetting.LastTest);

var foundTest = TestTypes.Find(t => t.FullName == lastTest);

Expand Down Expand Up @@ -479,7 +479,7 @@ private void finishLoad(TestScene newTest, Action onCompletion)

foreach (var m in newTest.GetType().GetMethods())
{
var name = m.Name;
string name = m.Name;

if (name == nameof(TestScene.TestConstructor) || m.GetCustomAttribute(typeof(IgnoreAttribute), false) != null)
continue;
Expand All @@ -491,7 +491,7 @@ private void finishLoad(TestScene newTest, Action onCompletion)

if (m.GetCustomAttribute(typeof(RepeatAttribute), false) != null)
{
var count = m.GetCustomAttributesData().Single(a => a.AttributeType == typeof(RepeatAttribute)).ConstructorArguments.Single().Value;
object count = m.GetCustomAttributesData().Single(a => a.AttributeType == typeof(RepeatAttribute)).ConstructorArguments.Single().Value;
Debug.Assert(count != null);

runCount += (int)count;
Expand Down Expand Up @@ -519,7 +519,7 @@ private void finishLoad(TestScene newTest, Action onCompletion)

List<object> choices = new List<object>();

foreach (var choice in valueAttrib.GetData(p))
foreach (object choice in valueAttrib.GetData(p))
choices.Add(choice);

valueMatrix.Add(choices);
Expand Down Expand Up @@ -558,13 +558,13 @@ private void finishLoad(TestScene newTest, Action onCompletion)
throw new InvalidOperationException($"The value of the source member {tcs.SourceName} must be non-null.");
}

foreach (var argument in sourceValue)
foreach (object argument in sourceValue)
{
hadTestAttributeTest = true;

if (argument is IEnumerable argumentsEnumerable)
{
var arguments = argumentsEnumerable.Cast<object>().ToArray();
object[] arguments = argumentsEnumerable.Cast<object>().ToArray();

CurrentTest.AddLabel($"{name}({string.Join(", ", arguments)}){repeatSuffix}");
handleTestMethod(m, arguments);
Expand Down Expand Up @@ -642,7 +642,7 @@ private static IEnumerable getTestCaseSourceValue(MethodInfo testMethod, TestCas
return (IEnumerable)sp.GetValue(null);

case MethodInfo sm:
var methodParamsLength = sm.GetParameters().Length;
int methodParamsLength = sm.GetParameters().Length;
if (methodParamsLength != (tcs.MethodParams?.Length ?? 0))
throw new InvalidOperationException($"The given source method parameters count doesn't match the method. (attribute has {tcs.MethodParams?.Length ?? 0}, method has {methodParamsLength})");

Expand Down

0 comments on commit 7a743d3

Please sign in to comment.