Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3b7adbd

Browse files
committedMar 15, 2018
Use pattern matching
In some cases I've inverted the early return to wrap/indent the remaining block of code as it reads much more nicely when using pattern matching than the negated form.
1 parent dc3fbb0 commit 3b7adbd

File tree

30 files changed

+249
-417
lines changed

30 files changed

+249
-417
lines changed
 

‎GitCommands/Core/SimpleStructured.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,13 @@ private static string ToString(object obj, string indent)
7979

8080
if (!(obj is string))
8181
{
82-
IEnumerable eo = obj as IEnumerable;
83-
if (eo != null)
82+
if (obj is IEnumerable eo)
8483
{
8584
return eo.Cast<object>().Select(o => ToString(o, indent + " ")).Join("\n");
8685
}
8786
}
8887

89-
SimpleStructured ss = obj as SimpleStructured;
90-
if (ss != null)
88+
if (obj is SimpleStructured ss)
9189
{
9290
return ToString(ss.InlinedStructure(), indent);
9391
}

‎GitCommands/Git/GitModule.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3759,8 +3759,7 @@ public override bool Equals(object obj)
37593759
return true;
37603760
}
37613761

3762-
GitModule other = obj as GitModule;
3763-
return (other != null) && Equals(other);
3762+
return obj is GitModule other && Equals(other);
37643763
}
37653764

37663765
private bool Equals(GitModule other)

‎GitCommands/Git/GitStash.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ public override bool Equals(object obj)
6464
return true;
6565
}
6666

67-
GitStash other = obj as GitStash;
68-
return other != null && Equals(other);
67+
return obj is GitStash other && Equals(other);
6968
}
7069

7170
protected bool Equals(GitStash other)

‎GitCommands/Git/GitSubmoduleInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public string Status
6565

6666
public override bool Equals(object obj)
6767
{
68-
return obj is GitSubmoduleInfo && this == (GitSubmoduleInfo)obj;
68+
return obj is GitSubmoduleInfo info && this == info;
6969
}
7070

7171
public override int GetHashCode()

‎GitCommands/Repository/Repositories.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ private static BindingList<RepositoryCategory> DeserializeRepositories(string xm
178178
stringReader = new StringReader(xml);
179179
using (var xmlReader = new XmlTextReader(stringReader))
180180
{
181-
var repos = serializer.Deserialize(xmlReader) as BindingList<RepositoryCategory>;
182-
if (repos != null)
181+
if (serializer.Deserialize(xmlReader) is BindingList<RepositoryCategory> repos)
183182
{
184183
repositories = new BindingList<RepositoryCategory>();
185184
foreach (var repositoryCategory in repos.Where(r => r.CategoryType == RepositoryCategoryType.Repositories))
@@ -235,8 +234,7 @@ private static RepositoryHistory DeserializeHistoryFromXml(string xml)
235234
using (var xmlReader = new XmlTextReader(stringReader))
236235
{
237236
stringReader = null;
238-
var obj = serializer.Deserialize(xmlReader) as RepositoryHistory;
239-
if (obj != null)
237+
if (serializer.Deserialize(xmlReader) is RepositoryHistory obj)
240238
{
241239
history = obj;
242240
history.SetIcon();

‎GitPluginShared/Commands/Blame.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ protected override void OnExecute(SelectedItem item, string fileName, OutputWind
88
{
99
string[] arguments = null;
1010

11-
var textSelection = item.DTE.ActiveDocument.Selection as TextSelection;
12-
if (textSelection != null)
11+
if (item.DTE.ActiveDocument.Selection is TextSelection textSelection)
1312
{
1413
arguments = new[] { textSelection.CurrentLine.ToString() };
1514
}

‎GitPluginShared/Git/GitCommands.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ private static string GetRegistryValue(RegistryKey root, string subkey, string k
1818

1919
string value = "";
2020

21-
if (rk != null && rk.GetValue(key) is string)
21+
if (rk != null && rk.GetValue(key) is string str)
2222
{
23-
value = rk.GetValue(key).ToString();
23+
value = str;
2424
rk.Flush();
2525
rk.Close();
2626
}

‎GitUI/CommandsDialogs/BrowseDialog/DashboardControl/Dashboard.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ private static T FindControl<T>(IEnumerable controls, Func<T, bool> predicate)
138138
{
139139
foreach (Control control in controls)
140140
{
141-
var result = control as T;
142-
143-
if (result != null && predicate(result))
141+
if (control is T result && predicate(result))
144142
{
145143
return result;
146144
}
@@ -275,8 +273,7 @@ private static void IssuesItem_Click(object sender, EventArgs e)
275273

276274
private void dashboardItem_Click(object sender, EventArgs e)
277275
{
278-
var label = sender as DashboardItem;
279-
if (label == null || string.IsNullOrEmpty(label.Path))
276+
if (!(sender is DashboardItem label) || string.IsNullOrEmpty(label.Path))
280277
{
281278
return;
282279
}
@@ -360,8 +357,7 @@ private void pictureBox1_Click(object sender, EventArgs e)
360357

361358
private void groupLayoutPanel_DragDrop(object sender, DragEventArgs e)
362359
{
363-
var fileNameArray = e.Data.GetData(DataFormats.FileDrop) as string[];
364-
if (fileNameArray != null)
360+
if (e.Data.GetData(DataFormats.FileDrop) is string[] fileNameArray)
365361
{
366362
if (fileNameArray.Length != 1)
367363
{
@@ -410,8 +406,7 @@ private void groupLayoutPanel_DragDrop(object sender, DragEventArgs e)
410406

411407
private void groupLayoutPanel_DragEnter(object sender, DragEventArgs e)
412408
{
413-
var fileNameArray = e.Data.GetData(DataFormats.FileDrop) as string[];
414-
if (fileNameArray != null)
409+
if (e.Data.GetData(DataFormats.FileDrop) is string[] fileNameArray)
415410
{
416411
if (fileNameArray.Length != 1)
417412
{

‎GitUI/CommandsDialogs/BrowseDialog/DashboardControl/DashboardCategory.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -286,29 +286,25 @@ public void Clear()
286286

287287
private void addToItem_Click(object sender, EventArgs e)
288288
{
289-
var toolStripItem = sender as ToolStripItem;
290-
291-
if (toolStripItem == null)
292-
{
293-
return;
294-
}
295-
296289
if (_repository == null)
297290
{
298291
return;
299292
}
300293

301-
foreach (RepositoryCategory newRepositoryCategory in Repositories.RepositoryCategories)
294+
if (sender is ToolStripItem toolStripItem)
302295
{
303-
if (newRepositoryCategory.Description.Equals(toolStripItem.Text))
296+
foreach (RepositoryCategory newRepositoryCategory in Repositories.RepositoryCategories)
304297
{
305-
RepositoryCategory.RemoveRepository(_repository);
306-
_repository.RepositoryType = RepositoryType.Repository;
307-
newRepositoryCategory.AddRepository(_repository);
298+
if (newRepositoryCategory.Description.Equals(toolStripItem.Text))
299+
{
300+
RepositoryCategory.RemoveRepository(_repository);
301+
_repository.RepositoryType = RepositoryType.Repository;
302+
newRepositoryCategory.AddRepository(_repository);
303+
}
308304
}
309-
}
310305

311-
dashboardCategoryChanged(this, null);
306+
dashboardCategoryChanged(this, null);
307+
}
312308
}
313309

314310
private void editToolStripMenuItem_Click(object sender, EventArgs e)

‎GitUI/CommandsDialogs/BrowseDialog/FormBrowseMenus.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ private void SetDropDownItems(ToolStripMenuItem toolStripMenuItemTarget, IEnumer
150150
var toolStripItem = MenuCommand.CreateToolStripItem(menuCommand);
151151
toolStripItems.Add(toolStripItem);
152152

153-
var toolStripMenuItem = toolStripItem as ToolStripMenuItem;
154-
if (toolStripMenuItem != null)
153+
if (toolStripItem is ToolStripMenuItem toolStripMenuItem)
155154
{
156155
menuCommand.RegisterMenuItem(toolStripMenuItem);
157156
_itemsRegisteredWithMenuCommand.Add(toolStripMenuItem);

‎GitUI/CommandsDialogs/FormBrowse.cs

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -478,28 +478,23 @@ private void ItemClick(object sender, EventArgs e)
478478
{
479479
var menuItem = sender as ToolStripMenuItem;
480480

481-
var plugin = menuItem?.Tag as IGitPlugin;
482-
if (plugin == null)
481+
if (menuItem?.Tag is IGitPlugin plugin)
483482
{
484-
return;
485-
}
486-
487-
var eventArgs = new GitUIEventArgs(this, UICommands);
483+
var eventArgs = new GitUIEventArgs(this, UICommands);
488484

489-
bool refresh = plugin.Execute(eventArgs);
490-
if (refresh)
491-
{
492-
RefreshToolStripMenuItemClick(null, null);
485+
bool refresh = plugin.Execute(eventArgs);
486+
if (refresh)
487+
{
488+
RefreshToolStripMenuItemClick(null, null);
489+
}
493490
}
494491
}
495492

496493
private void UpdatePluginMenu(bool validWorkingDir)
497494
{
498495
foreach (ToolStripItem item in pluginsToolStripMenuItem.DropDownItems)
499496
{
500-
var plugin = item.Tag as IGitPluginForRepository;
501-
502-
item.Enabled = plugin == null || validWorkingDir;
497+
item.Enabled = !(item.Tag is IGitPluginForRepository) || validWorkingDir;
503498
}
504499
}
505500

@@ -1535,8 +1530,7 @@ private void ManageSubmodulesToolStripMenuItemClick(object sender, EventArgs e)
15351530

15361531
private void UpdateSubmoduleToolStripMenuItemClick(object sender, EventArgs e)
15371532
{
1538-
var toolStripMenuItem = sender as ToolStripMenuItem;
1539-
if (toolStripMenuItem != null)
1533+
if (sender is ToolStripMenuItem toolStripMenuItem)
15401534
{
15411535
var submodule = toolStripMenuItem.Tag as string;
15421536
FormProcess.ShowDialog(this, Module.SuperprojectModule, GitCommandHelpers.SubmoduleUpdateCmd(submodule));
@@ -1640,14 +1634,10 @@ private void ChangeWorkingDir(string path)
16401634

16411635
private void HistoryItemMenuClick(object sender, EventArgs e)
16421636
{
1643-
var button = sender as ToolStripMenuItem;
1644-
1645-
if (button == null)
1637+
if (sender is ToolStripMenuItem button)
16461638
{
1647-
return;
1639+
ChangeWorkingDir(button.Text);
16481640
}
1649-
1650-
ChangeWorkingDir(button.Text);
16511641
}
16521642

16531643
private void ClearRecentRepositoriesListClick(object sender, EventArgs e)
@@ -2381,8 +2371,7 @@ private void RevisionInfo_CommandClick(object sender, CommitInfo.CommandEventArg
23812371

23822372
private void SubmoduleToolStripButtonClick(object sender, EventArgs e)
23832373
{
2384-
var menuSender = sender as ToolStripMenuItem;
2385-
if (menuSender != null)
2374+
if (sender is ToolStripMenuItem menuSender)
23862375
{
23872376
SetWorkingDir(menuSender.Tag as string);
23882377
}
@@ -2402,24 +2391,16 @@ private void PreventToolStripSplitButtonClosing(ToolStripSplitButton control)
24022391

24032392
private static void ToolStripSplitButtonDropDownClosed(object sender, EventArgs e)
24042393
{
2405-
var control = sender as ToolStripSplitButton;
2406-
2407-
if (control == null)
2394+
if (sender is ToolStripSplitButton control)
24082395
{
2409-
return;
2410-
}
2411-
2412-
control.DropDownClosed -= ToolStripSplitButtonDropDownClosed;
2413-
2414-
var controlToFocus = control.Tag as Control;
2396+
control.DropDownClosed -= ToolStripSplitButtonDropDownClosed;
24152397

2416-
if (controlToFocus == null)
2417-
{
2418-
return;
2398+
if (control.Tag is Control controlToFocus)
2399+
{
2400+
controlToFocus.Focus();
2401+
control.Tag = null;
2402+
}
24192403
}
2420-
2421-
controlToFocus.Focus();
2422-
control.Tag = null;
24232404
}
24242405

24252406
private void toolStripButtonLevelUp_DropDownOpening(object sender, EventArgs e)
@@ -2432,8 +2413,7 @@ private void RemoveSubmoduleButtons()
24322413
{
24332414
foreach (var item in toolStripButtonLevelUp.DropDownItems)
24342415
{
2435-
var toolStripButton = item as ToolStripMenuItem;
2436-
if (toolStripButton != null)
2416+
if (item is ToolStripMenuItem toolStripButton)
24372417
{
24382418
toolStripButton.Click -= SubmoduleToolStripButtonClick;
24392419
}

‎GitUI/CommandsDialogs/FormPush.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,9 +724,7 @@ private void BranchSelectedValueChanged(object sender, EventArgs e)
724724
{
725725
if (PushToRemote.Checked)
726726
{
727-
var branch = _NO_TRANSLATE_Branch.SelectedItem as GitRef;
728-
729-
if (branch != null)
727+
if (_NO_TRANSLATE_Branch.SelectedItem is GitRef branch)
730728
{
731729
if (_selectedRemote != null)
732730
{

‎GitUI/CommandsDialogs/RepoHosting/CreatePullRequestForm.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ private void LoadRemotes(IHostedRemote[] foreignHostedRemotes)
7979
{
8080
for (int i = 0; i < _pullReqTargetsCB.Items.Count; i++)
8181
{
82-
var ihr = _pullReqTargetsCB.Items[i] as IHostedRemote;
83-
if (ihr != null && ihr.Name == _chooseRemote)
82+
if (_pullReqTargetsCB.Items[i] is IHostedRemote ihr && ihr.Name == _chooseRemote)
8483
{
8584
_pullReqTargetsCB.SelectedIndex = i;
8685
break;

0 commit comments

Comments
 (0)
Please sign in to comment.