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

Use Enumerable.Empty and Array.Empty #4654

Merged
merged 1 commit into from
Mar 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion GitCommands/Config/ConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public IList<string> GetValues(string setting)

if (configSection == null)
{
return new List<string>();
return Array.Empty<string>();
}

return configSection.GetValues(keyName);
Expand Down
4 changes: 2 additions & 2 deletions GitCommands/ExternalLinks/ExternalLinksLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static IList<ExternalLinkDefinition> LoadFromXmlString(string xmlString)
{
if (string.IsNullOrWhiteSpace(xmlString))
{
return new List<ExternalLinkDefinition>();
return Array.Empty<ExternalLinkDefinition>();
}

try
Expand All @@ -91,7 +91,7 @@ private static IList<ExternalLinkDefinition> LoadFromXmlString(string xmlString)
Debug.WriteLine(ex.Message);
}

return new List<ExternalLinkDefinition>();
return Array.Empty<ExternalLinkDefinition>();
}
}
}
12 changes: 5 additions & 7 deletions GitCommands/Git/GitModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2119,11 +2119,9 @@ public IList<PatchFile> GetRebasePatchFiles()

int.TryParse(nextFile, out var next);

var files = new string[0];
if (Directory.Exists(GetRebaseDir()))
{
files = Directory.GetFiles(GetRebaseDir());
}
var files = Directory.Exists(GetRebaseDir())
? Directory.GetFiles(GetRebaseDir())
: Array.Empty<string>();

foreach (var fullFileName in files)
{
Expand Down Expand Up @@ -3037,7 +3035,7 @@ public IEnumerable<string> GetAllBranchesWhichContainGivenCommit(string sha1, bo
string info = RunGitCmd("branch " + args);
if (info.Trim().StartsWith("fatal") || info.Trim().StartsWith("error:"))
{
return new List<string>();
return Enumerable.Empty<string>();
}

string[] result = info.Split(new[] { '\r', '\n', '*' }, StringSplitOptions.RemoveEmptyEntries);
Expand Down Expand Up @@ -3068,7 +3066,7 @@ public IEnumerable<string> GetAllTagsWhichContainGivenCommit(string sha1)

if (info.Trim().StartsWith("fatal") || info.Trim().StartsWith("error:"))
{
return new List<string>();
return Enumerable.Empty<string>();
}

return info.Split(new[] { '\r', '\n', '*', ' ' }, StringSplitOptions.RemoveEmptyEntries);
Expand Down
4 changes: 2 additions & 2 deletions GitUI/CommandsDialogs/BrowseDialog/FormBrowseMenus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private void CreateAdditionalMainMenuItems()

private IEnumerable<Tuple<string, object>> GetAdditionalMainMenuItemsForTranslation()
{
var list = new List<ToolStripMenuItem> { _navigateToolStripMenuItem, _viewToolStripMenuItem };
var list = new[] { _navigateToolStripMenuItem, _viewToolStripMenuItem };
return list.Select(menuItem => new Tuple<string, object>(menuItem.Name, menuItem));
}

Expand Down Expand Up @@ -199,7 +199,7 @@ private IEnumerable<MenuCommand> GetNavigateAndViewMenuCommands()
{
if (_navigateMenuCommands == null && _viewMenuCommands == null)
{
return new List<MenuCommand>();
return Enumerable.Empty<MenuCommand>();
}
else if (_navigateMenuCommands != null && _viewMenuCommands != null)
{
Expand Down
6 changes: 3 additions & 3 deletions GitUI/CommandsDialogs/RepoHosting/ViewPullRequestsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public ViewPullRequestsForm(GitUICommands commands, IRepositoryHostPlugin gitHos
_gitHoster = gitHoster;
}

private List<IHostedRemote> _hostedRemotes;
private List<IPullRequestInformation> _pullRequestsInfo;
private IReadOnlyList<IHostedRemote> _hostedRemotes;
private IReadOnlyList<IPullRequestInformation> _pullRequestsInfo;
private IPullRequestInformation _currentPullRequestInfo;

private void ViewPullRequestsForm_Load(object sender, EventArgs e)
Expand Down Expand Up @@ -118,7 +118,7 @@ private void _selectedOwner_SelectedIndexChanged(object sender, EventArgs e)
_strError.Text));
}

private void SetPullRequestsData(List<IPullRequestInformation> infos)
private void SetPullRequestsData(IReadOnlyList<IPullRequestInformation> infos)
{
if (_isFirstLoad)
{
Expand Down
2 changes: 1 addition & 1 deletion GitUI/UserControls/RevisionGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,7 @@ private void GitGetCommitsCommandExited(object sender, EventArgs e)
private void SelectInitialRevision()
{
string filtredCurrentCheckout = _filtredCurrentCheckout;
string[] lastSelectedRows = LastSelectedRows ?? new string[0];
string[] lastSelectedRows = LastSelectedRows ?? Array.Empty<string>();

// filter out all unavailable commits from LastSelectedRows.
lastSelectedRows = lastSelectedRows.Where(revision => FindRevisionIndex(revision) >= 0).ToArray();
Expand Down
12 changes: 6 additions & 6 deletions Plugins/GitFlow/GitFlowForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public partial class GitFlowForm : GitExtensionsFormBase

private readonly GitUIBaseEventArgs _gitUiCommands;

private Dictionary<string, List<string>> Branches { get; }
private Dictionary<string, IReadOnlyList<string>> Branches { get; } = new Dictionary<string, IReadOnlyList<string>>();

private readonly AsyncLoader _task = new AsyncLoader();

Expand Down Expand Up @@ -51,8 +51,6 @@ public GitFlowForm(GitUIBaseEventArgs gitUiCommands)

_gitUiCommands = gitUiCommands;

Branches = new Dictionary<string, List<string>>();

lblPrefixManage.Text = string.Empty;
ttGitFlow.SetToolTip(lnkGitFlow, _gitFlowTooltip.Text);

Expand Down Expand Up @@ -128,15 +126,17 @@ private void LoadBranches(string branchType)
}
}

private List<string> GetBranches(string typeBranch)
private IReadOnlyList<string> GetBranches(string typeBranch)
{
var result = _gitUiCommands.GitModule.RunGitCmdResult("flow " + typeBranch);

if (result.ExitCode != 0)
{
return new List<string>();
return Array.Empty<string>();
}

string[] references = result.StdOutput.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

return references.Select(e => e.Trim('*', ' ', '\n', '\r')).ToList();
}

Expand All @@ -155,7 +155,7 @@ private void DisplayBranchDatas()
var isThereABranch = branches.Any();

cbManageType.Enabled = true;
cbBranches.DataSource = isThereABranch ? branches : new List<string> { string.Format(_noBranchExist.Text, branchType) };
cbBranches.DataSource = isThereABranch ? branches : new[] { string.Format(_noBranchExist.Text, branchType) };
cbBranches.Enabled = isThereABranch;
if (isThereABranch && CurrentBranch != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface IHostedRepository
/// <returns>The new repo, owne by me.</returns>
IHostedRepository Fork();

List<IPullRequestInformation> GetPullRequests();
IReadOnlyList<IPullRequestInformation> GetPullRequests();

/// <returns>Pull request number</returns>
int CreatePullRequest(string myBranch, string remoteBranch, string title, string body);
Expand Down
8 changes: 5 additions & 3 deletions Plugins/Github3/GithubRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,24 @@ public IHostedRepository Fork()
return new GithubRepo(_repo.CreateFork());
}

public List<IPullRequestInformation> GetPullRequests()
public IReadOnlyList<IPullRequestInformation> GetPullRequests()
{
var pullRequests = _repo?.GetPullRequests();

if (pullRequests != null)
{
return pullRequests
.Select(pr => (IPullRequestInformation)new GithubPullRequest(pr))
.Select(pr => new GithubPullRequest(pr))
.ToList();
}

return new List<IPullRequestInformation>();
return Array.Empty<IPullRequestInformation>();
}

public int CreatePullRequest(string myBranch, string remoteBranch, string title, string body)
{
var pullrequest = _repo.CreatePullRequest(GithubLoginInfo.Username + ":" + myBranch, remoteBranch, title, body);

if (pullrequest == null || pullrequest.Number == 0)
{
throw new Exception("Failed to create pull request.");
Expand Down
3 changes: 2 additions & 1 deletion ResourceManager/GitPluginBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using GitCommands;
using GitUIPluginInterfaces;

Expand All @@ -22,7 +23,7 @@ protected void SetNameAndDescription(string name)

public virtual IEnumerable<ISetting> GetSettings()
{
return new List<ISetting>();
return Enumerable.Empty<ISetting>();
}

public virtual void Register(IGitUICommands gitUiCommands)
Expand Down
2 changes: 1 addition & 1 deletion ResourceManager/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static string[] GetAllTranslations()
string translationDir = GetTranslationDir();
if (!Directory.Exists(translationDir))
{
return new string[0];
return Array.Empty<string>();
}

foreach (string fileName in Directory.GetFiles(translationDir, "*.xlf"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void Given_previously_selected_revision_When_no_revision_selected_Then_Pr
currentModule.SetSetting(SettingKeyString.UserEmail, ExpectedAuthorEmail2);
sut.ProcessRevisionSelectionChange(currentModule, new[] { NewRevisionWithAuthorEmail(ExpectedAuthorEmail1) });

var action = sut.ProcessRevisionSelectionChange(currentModule, new GitRevision[0]);
var action = sut.ProcessRevisionSelectionChange(currentModule, Array.Empty<GitRevision>());

action.Should().Be(AuthorEmailBasedRevisionHighlighting.SelectionChangeAction.RefreshUserInterface);
}
Expand All @@ -147,7 +147,7 @@ public void Given_previously_selected_revision_When_no_revision_selected_Then_Au
currentModule.SetSetting(SettingKeyString.UserEmail, ExpectedAuthorEmail2);
sut.ProcessRevisionSelectionChange(currentModule, new[] { NewRevisionWithAuthorEmail(ExpectedAuthorEmail1) });

sut.ProcessRevisionSelectionChange(currentModule, new GitRevision[0]);
sut.ProcessRevisionSelectionChange(currentModule, Array.Empty<GitRevision>());

sut.AuthorEmailToHighlight.Should().Be(ExpectedAuthorEmail2);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GitCommands;
using System;
using GitCommands;
using GitUI.UserControls.ToolStripClasses;
using NUnit.Framework;

Expand Down Expand Up @@ -31,7 +32,7 @@ public void SetUp()
[Test]
public void ReturnsIconCleanWhenThereIsNoChangedFiles()
{
var commitIcon = _commitIconProvider.GetCommitIcon(new GitItemStatus[0]);
var commitIcon = _commitIconProvider.GetCommitIcon(Array.Empty<GitItemStatus>());

Assert.AreEqual(CommitIconProvider.IconClean, commitIcon);
}
Expand Down