Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inline out variables #6193

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions build/Shared/EqualityUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ internal static bool OrderedEquals<TSource, TKey>(this IEnumerable<TSource>? sel
Debug.Assert(orderComparer != null || typeof(TKey) != typeof(string), "Argument " + "orderComparer" + " must be provided if " + "TKey" + " is a string.");
Debug.Assert(sequenceComparer != null || typeof(TSource) != typeof(string), "Argument " + "sequenceComparer" + " must be provided if " + "TSource" + " is a string.");

bool identityEquals;
if (TryIdentityEquals(self, other, out identityEquals))
if (TryIdentityEquals(self, other, out var identityEquals))
{
return identityEquals;
}
Expand All @@ -54,8 +53,7 @@ internal static bool OrderedEquals<TSource, TKey>(this ICollection<TSource>? sel
Debug.Assert(orderComparer != null || typeof(TKey) != typeof(string), "Argument " + "orderComparer" + " must be provided if " + "TKey" + " is a string.");
Debug.Assert(sequenceComparer != null || typeof(TSource) != typeof(string), "Argument " + "sequenceComparer" + " must be provided if " + "TSource" + " is a string.");

bool identityEquals;
if (TryIdentityEquals(self, other, out identityEquals))
if (TryIdentityEquals(self, other, out var identityEquals))
{
return identityEquals;
}
Expand Down Expand Up @@ -96,8 +94,7 @@ internal static bool OrderedEquals<TSource, TKey>(this IList<TSource>? self, ILi
Debug.Assert(orderComparer != null || typeof(TKey) != typeof(string), "Argument " + "orderComparer" + " must be provided if " + "TKey" + " is a string.");
Debug.Assert(sequenceComparer != null || typeof(TSource) != typeof(string), "Argument " + "sequenceComparer" + " must be provided if " + "TSource" + " is a string.");

bool identityEquals;
if (TryIdentityEquals(self, other, out identityEquals))
if (TryIdentityEquals(self, other, out var identityEquals))
{
return identityEquals;
}
Expand Down Expand Up @@ -131,8 +128,7 @@ internal static bool SequenceEqualWithNullCheck<T>(
IEnumerable<T>? other,
IEqualityComparer<T>? comparer = null)
{
bool identityEquals;
if (TryIdentityEquals(self, other, out identityEquals))
if (TryIdentityEquals(self, other, out var identityEquals))
{
return identityEquals;
}
Expand All @@ -154,8 +150,7 @@ internal static bool SequenceEqualWithNullCheck<T>(
ICollection<T>? other,
IEqualityComparer<T>? comparer = null)
{
bool identityEquals;
if (TryIdentityEquals(self, other, out identityEquals))
if (TryIdentityEquals(self, other, out var identityEquals))
{
return identityEquals;
}
Expand Down Expand Up @@ -187,8 +182,7 @@ internal static bool SequenceEqualWithNullCheck<T>(
IList<T>? other,
IEqualityComparer<T>? comparer = null)
{
bool identityEquals;
if (TryIdentityEquals(self, other, out identityEquals))
if (TryIdentityEquals(self, other, out var identityEquals))
{
return identityEquals;
}
Expand Down Expand Up @@ -225,8 +219,7 @@ internal static bool SetEqualsWithNullCheck<T>(
ISet<T>? other,
IEqualityComparer<T>? comparer = null)
{
bool identityEquals;
if (TryIdentityEquals(self, other, out identityEquals))
if (TryIdentityEquals(self, other, out var identityEquals))
{
return identityEquals;
}
Expand Down Expand Up @@ -262,8 +255,7 @@ internal static bool DictionaryEquals<TKey, TValue>(
Func<TValue, TValue, bool> comparerFunc = (s, o) => comparer.Equals(s, o);
compareValues = compareValues ?? comparerFunc;

bool identityEquals;
if (TryIdentityEquals(self, other, out identityEquals))
if (TryIdentityEquals(self, other, out var identityEquals))
{
return identityEquals;
}
Expand Down Expand Up @@ -312,8 +304,7 @@ internal static bool DictionaryOfSequenceEquals<TKey, TValue>(

internal static bool EqualsWithNullCheck<T>(T self, T other)
{
bool identityEquals;
if (TryIdentityEquals(self, other, out identityEquals))
if (TryIdentityEquals(self, other, out var identityEquals))
{
return identityEquals;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ public override void ExecuteCommand()

if (!string.IsNullOrEmpty(Version))
{
NuGetVersion version;
if (!NuGetVersion.TryParse(Version, out version))
if (!NuGetVersion.TryParse(Version, out var version))
{
throw new PackagingException(NuGetLogCode.NU5010, string.Format(CultureInfo.CurrentCulture, NuGetResources.InstallCommandPackageReferenceInvalidVersion, Version));
}
Expand Down
13 changes: 4 additions & 9 deletions src/NuGet.Clients/NuGet.CommandLine/Commands/ProjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,7 @@ public string InitializeProperties(Packaging.IPackageMetadata metadata)

public string GetPropertyValue(string propertyName)
{
string value;
if (!_properties.TryGetValue(propertyName, out value) &&
if (!_properties.TryGetValue(propertyName, out var value) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a violation of our repo's coding guideline number 10: https://github.com/NuGet/NuGet.Client/blob/dev/docs/coding-guidelines.md

it's not obvious what value's type is, so it shouldn't be var. While I'm not a big fan of this coding guideline (if I really care, I'll open VS and use intellisense), it's what the team voted for (to make reviewing PRs easiler, maybe?).

Searching GitHub's PR page for var out, there are over 300 instances, and I expect most of them will be "non-obvious" and therefore should be changed if we follow the coding guidelines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zivkan given that convention. can we merge this one first #6202

!ProjectProperties.TryGetValue(propertyName, out value))
{
dynamic property = _project.GetProperty(propertyName);
Expand Down Expand Up @@ -555,9 +554,7 @@ private void ExtractMetadataFromProject(Packaging.PackageBuilder builder)
string version = _project.GetPropertyValue("Version");
if (builder.Version == null)
{
NuGetVersion parsedVersion;

if (NuGetVersion.TryParse(version, out parsedVersion))
if (NuGetVersion.TryParse(version, out var parsedVersion))
{
builder.Version = parsedVersion;
}
Expand Down Expand Up @@ -658,8 +655,7 @@ private static bool ShouldExcludeItem(dynamic item)

if (item.HasMetadata(ReferenceOutputAssembly))
{
bool result;
if (bool.TryParse(item.GetMetadataValue("ReferenceOutputAssembly"), out result))
if (bool.TryParse(item.GetMetadataValue("ReferenceOutputAssembly"), out bool result))
{
if (!result)
{
Expand Down Expand Up @@ -1172,8 +1168,7 @@ private static void ProcessTransformFiles(PackageBuilder builder, IEnumerable<IP

foreach (var transformGroup in transformGroups)
{
IPackageFile file;
if (fileLookup.TryGetValue(transformGroup.Key, out file))
if (fileLookup.TryGetValue(transformGroup.Key, out var file))
{
// Replace the original file with a file that removes the transforms
builder.Files.Remove(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ internal static class DependencyBehaviorHelper
{
private static DependencyBehavior TryGetDependencyBehavior(string behaviorStr)
{
DependencyBehavior dependencyBehavior;

if (!Enum.TryParse<DependencyBehavior>(behaviorStr, ignoreCase: true, result: out dependencyBehavior) ||
if (!Enum.TryParse<DependencyBehavior>(behaviorStr, ignoreCase: true, result: out var dependencyBehavior) ||
!Enum.IsDefined(typeof(DependencyBehavior), dependencyBehavior))
{
throw new CommandException(string.Format(CultureInfo.CurrentCulture,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public static string GetLocalizedString(Type resourceType, string resourceNames)
_cachedManagers = new Dictionary<Type, ResourceManager>();
}

ResourceManager resourceManager;
if (!_cachedManagers.TryGetValue(resourceType, out resourceManager))
if (!_cachedManagers.TryGetValue(resourceType, out var resourceManager))
{
PropertyInfo property = resourceType.GetProperty("ResourceManager", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);

Expand Down
3 changes: 1 addition & 2 deletions src/NuGet.Clients/NuGet.CommandLine/MsBuildToolset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ public static string GetMsBuildDirFromVsDir(string vsDir)
private static float ToFloatValue(string directoryName)
{
var dirName = new DirectoryInfo(directoryName).Name;
float dirValue;
if (float.TryParse(dirName, NumberStyles.Float, CultureInfo.InvariantCulture, out dirValue))
if (float.TryParse(dirName, NumberStyles.Float, CultureInfo.InvariantCulture, out var dirValue))
{
return dirValue;
}
Expand Down
9 changes: 3 additions & 6 deletions src/NuGet.Clients/NuGet.CommandLine/MsBuildUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -809,10 +809,8 @@ private static MsBuildToolset GetToolsetFromUserVersion(
// without the safe fallback to 0.0 built into t.ParsedToolsVersion.
selectedToolset = installedToolsets.FirstOrDefault(t =>
{
Version parsedUserVersion;
Version parsedToolsVersion;
if (Version.TryParse(userVersionString, out parsedUserVersion) &&
Version.TryParse(t.Version, out parsedToolsVersion))
if (Version.TryParse(userVersionString, out var parsedUserVersion) &&
Version.TryParse(t.Version, out var parsedToolsVersion))
{
return parsedToolsVersion.Major == parsedUserVersion.Major &&
parsedToolsVersion.Minor == parsedUserVersion.Minor;
Expand Down Expand Up @@ -945,8 +943,7 @@ private static List<MsBuildToolset> GetInstalledSxsToolsets()
while (true)
{
var fetchedInstances = new ISetupInstance[3];
int fetched;
enumerator.Next(fetchedInstances.Length, fetchedInstances, out fetched);
enumerator.Next(fetchedInstances.Length, fetchedInstances, out var fetched);
if (fetched == 0)
{
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ private bool TryGetCredentials(Uri uri, out ICredentials configurationCredential
{
var source = _packageSourceProvider.LoadPackageSources().FirstOrDefault(p =>
{
Uri sourceUri;
return p.Credentials != null
&& p.Credentials.IsValid()
&& Uri.TryCreate(p.Source, UriKind.Absolute, out sourceUri)
&& UriEquals(sourceUri, uri);
&& p.Credentials.IsValid()
&& Uri.TryCreate(p.Source, UriKind.Absolute, out var sourceUri)
&& UriEquals(sourceUri, uri);
});
if (source == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ private static TextFormattingRunProperties GetFormat(Color? foreground, Color? b
public IClassificationType GetClassificationType(Color? foreground, Color? background)
{
var key = Tuple.Create(foreground, background);
IClassificationType classificationType;
if (!_classificationMap.TryGetValue(key, out classificationType))
if (!_classificationMap.TryGetValue(key, out var classificationType))
{
string classificationName = GetClassificationName(foreground, background);
classificationType = Factory.ClassificationTypeRegistryService.GetClassificationType(classificationName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ public OutputConsole(
fClearWithSolution: 0);
ErrorHandler.ThrowOnFailure(hr);

IVsOutputWindowPane pane;
hr = _vsOutputWindow.GetPane(
ref outputWindowPaneId,
out pane);
out var pane);
ErrorHandler.ThrowOnFailure(hr);

GuidList.NuGetOutputWindowPaneGuid = outputWindowPaneId;
Expand Down
6 changes: 2 additions & 4 deletions src/NuGet.Clients/NuGet.Console/WpfConsole/WpfConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,8 @@ private IWpfTextViewHost WpfTextViewHost
get
{
var userData = VsTextView as IVsUserData;
object data;
Guid guidIWpfTextViewHost = EditorDefGuidList.guidIWpfTextViewHost;
userData.GetData(ref guidIWpfTextViewHost, out data);
userData.GetData(ref guidIWpfTextViewHost, out var data);
var wpfTextViewHost = data as IWpfTextViewHost;

return wpfTextViewHost;
Expand Down Expand Up @@ -320,9 +319,8 @@ public IVsTextView VsTextView
var propCategoryContainer = _view as IVsTextEditorPropertyCategoryContainer;
if (propCategoryContainer != null)
{
IVsTextEditorPropertyContainer propContainer;
Guid guidPropCategory = EditorDefGuidList.guidEditPropCategoryViewMasterSettings;
int hr = propCategoryContainer.GetPropertyCategory(ref guidPropCategory, out propContainer);
int hr = propCategoryContainer.GetPropertyCategory(ref guidPropCategory, out var propContainer);
if (hr == 0)
{
propContainer.SetProperty(VSEDITPROPID.VSEDITPROPID_ViewGeneral_FontCategory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
/// </remarks>
private IList<ClassificationSpan> GetCommandLineClassifications(ITextSnapshot snapshot, IList<Span> cmdSpans)
{
IList<ClassificationSpan> cachedCommandLineClassifications;
if (TryGetCachedCommandLineClassifications(snapshot, cmdSpans, out cachedCommandLineClassifications))
if (TryGetCachedCommandLineClassifications(snapshot, cmdSpans, out var cachedCommandLineClassifications))
{
return cachedCommandLineClassifications;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public void AugmentCompletionSession(ICompletionSession session, IList<Completio
return;
}

SimpleExpansion simpleExpansion;
if (session.Properties.TryGetProperty("TabExpansion", out simpleExpansion))
if (session.Properties.TryGetProperty("TabExpansion", out SimpleExpansion simpleExpansion))
{
List<Completion> completions = new List<Completion>();
foreach (string s in simpleExpansion.Expansions)
Expand Down
3 changes: 1 addition & 2 deletions src/NuGet.Clients/NuGet.Indexing/NuGetQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public static Query MakeQuery(string q)
var grouping = new Dictionary<string, HashSet<string>>();
foreach (Clause clause in MakeClauses(Tokenize(q)))
{
HashSet<string> text;
if (!grouping.TryGetValue(clause.Field, out text))
if (!grouping.TryGetValue(clause.Field, out var text))
{
text = new HashSet<string>();
grouping.Add(clause.Field, text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ public void MergeResults(IEnumerable<IPackageSearchMetadata> result)
{
foreach (var entry in result)
{
IPackageSearchMetadata value;
if (_index.TryGetValue(entry.Identity.Id, out value))
if (_index.TryGetValue(entry.Identity.Id, out var value))
{
_index[entry.Identity.Id] = _splicer.MergeEntries(value, entry);
}
Expand Down
3 changes: 1 addition & 2 deletions src/NuGet.Clients/NuGet.Indexing/SemanticVersionFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public override bool IncrementToken()

string version = _termAttribute.Term;

NuGetVersion nuGetVersion;
if (NuGetVersion.TryParse(version, out nuGetVersion))
if (NuGetVersion.TryParse(version, out var nuGetVersion))
{
version = nuGetVersion.ToNormalizedString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public NuGet.SemanticVersion Version

if (nVersion != null)
{
NuGet.SemanticVersion sVersion;
_ = NuGet.SemanticVersion.TryParse(nVersion.ToNormalizedString(), out sVersion);
_ = NuGet.SemanticVersion.TryParse(nVersion.ToNormalizedString(), out var sVersion);
return sVersion;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,8 @@ private SourceValidationResult CheckSourceValidity(string inputSource)
Uri sourceUri;
if (Uri.TryCreate(source, UriKind.Relative, out sourceUri))
{
string outputPath;
bool? exists;
string errorMessage;
if (PSPathUtility.TryTranslatePSPath(SessionState, source, out outputPath, out exists, out errorMessage) &&
if (PSPathUtility.TryTranslatePSPath(SessionState, source, out var outputPath, out var exists, out errorMessage) &&
exists == true)
{
source = outputPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,7 @@ protected virtual DependencyBehavior GetDependencyBehavior()
protected DependencyBehavior GetDependencyBehaviorFromConfig()
{
var dependencySetting = SettingsUtility.GetConfigValue(ConfigSettings, ConfigurationConstants.DependencyVersion);
DependencyBehavior behavior;
var success = Enum.TryParse(dependencySetting, ignoreCase: true, result: out behavior);
var success = Enum.TryParse(dependencySetting, ignoreCase: true, result: out DependencyBehavior behavior);
if (success)
{
return behavior;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ private ComplexCommand ComplexCommand
{
_complexCommand = new ComplexCommand((allLines, lastLine) =>
{
Collection<PSParseError> errors;
PSParser.Tokenize(allLines, out errors);
PSParser.Tokenize(allLines, out var errors);

// If there is a parse error token whose END is past input END, consider
// it a multi-line command.
Expand Down Expand Up @@ -569,8 +568,7 @@ public bool Execute(IConsole console, string command, params object[] inputs)

ActiveConsole = console;

string fullCommand;
if (ComplexCommand.AddLine(command, out fullCommand)
if (ComplexCommand.AddLine(command, out var fullCommand)
&& !string.IsNullOrEmpty(fullCommand))
{
// create a new token source with each command since CTS aren't usable once cancelled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ public static bool TryTranslatePSPath(SessionState session, string psPath, out s
// we do not glob wildcards (literalpath.)
exists = session.InvokeProvider.Item.Exists(psPath, force: false, literalPath: true);

ProviderInfo provider;
PSDriveInfo drive;

// translate pspath, not trying to glob.
path = session.Path.GetUnresolvedProviderPathFromPSPath(psPath, out provider, out drive);
path = session.Path.GetUnresolvedProviderPathFromPSPath(psPath, out var provider, out drive);

// ensure path is on the filesystem (not registry, certificate, variable etc.)
if (provider.ImplementingType != typeof(FileSystemProvider))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public static NuGetVersion GetNuGetVersionFromString(string version)
throw new ArgumentNullException(nameof(version));
}

NuGetVersion nVersion;
var success = NuGetVersion.TryParse(version, out nVersion);
var success = NuGetVersion.TryParse(version, out var nVersion);
if (!success)
{
throw new InvalidOperationException(
Expand Down
Loading
Loading