diff options
author | Karsten Heimrich <karsten.heimrich@qt.io> | 2022-02-08 15:44:38 +0100 |
---|---|---|
committer | Karsten Heimrich <karsten.heimrich@qt.io> | 2022-02-09 18:54:53 +0000 |
commit | 134748d4634c5005fa9e4e97994c921660388b01 (patch) | |
tree | 9e0aad1378967cf46a8afcc9842d2028c2f331da | |
parent | 6257be9d2b29a9f9dc7535179233ab3c6b46bf60 (diff) |
Use pattern matching
Change-Id: I49c5c7988a08affc42455355926cc0e442e0fe5f
Reviewed-by: Miguel Costa <miguel.costa@qt.io>
-rw-r--r-- | QtVsTools.Core/HelperFunctions.cs | 40 | ||||
-rw-r--r-- | QtVsTools.Core/MsBuildProject.cs | 3 | ||||
-rw-r--r-- | QtVsTools.Core/ProjectExporter.cs | 8 | ||||
-rw-r--r-- | QtVsTools.Core/QtProject.cs | 42 | ||||
-rw-r--r-- | QtVsTools.Package/Common/Json/Serializable.cs | 6 | ||||
-rw-r--r-- | QtVsTools.Package/Common/Json/Serializer.cs | 3 | ||||
-rw-r--r-- | QtVsTools.Package/Options/QtOptionsPage.cs | 4 | ||||
-rw-r--r-- | QtVsTools.Package/Package/DteEventsHandler.cs | 6 | ||||
-rw-r--r-- | QtVsTools.Package/QML/Debugging/AD7/QmlDebugAD7Engine.cs | 3 | ||||
-rw-r--r-- | QtVsTools.Package/QML/Debugging/AD7/QmlDebugAD7InfoHelpers.cs | 3 | ||||
-rw-r--r-- | QtVsTools.Package/QtMsBuild/QtModulesPopup.xaml.cs | 3 | ||||
-rw-r--r-- | QtVsTools.Package/VisualStudio/VsShell.cs | 18 |
12 files changed, 56 insertions, 83 deletions
diff --git a/QtVsTools.Core/HelperFunctions.cs b/QtVsTools.Core/HelperFunctions.cs index 12a4933f..19dcba65 100644 --- a/QtVsTools.Core/HelperFunctions.cs +++ b/QtVsTools.Core/HelperFunctions.cs @@ -394,21 +394,17 @@ namespace QtVsTools.Core /// <returns></returns> public static VCCustomBuildTool GetCustomBuildTool(VCFileConfiguration config) { - var file = config.File as VCFile; - if (file == null || file.ItemType != "CustomBuild") - return null; - - var tool = config.Tool as VCCustomBuildTool; - if (tool == null) - return null; - - try { - // TODO: The return value is not used at all? - var cmdLine = tool.CommandLine; - } catch { - return null; + if (config.File is VCFile file + && file.ItemType == "CustomBuild" + && config.Tool is VCCustomBuildTool tool) { + try { + _ = tool.CommandLine; + } catch { + return null; + } + return tool; } - return tool; + return null; } /// <summary> @@ -1098,8 +1094,8 @@ namespace QtVsTools.Core return null; // don't handle multiple selection... use the first one - if (prjs.GetValue(0) is Project) - return (Project)prjs.GetValue(0); + if (prjs.GetValue(0) is Project project) + return project; return null; } @@ -1458,12 +1454,10 @@ namespace QtVsTools.Core VCProject vcProj = null; VCFile vcFile = null; string configName = "", platformName = ""; - var vcConfig = config as VCConfiguration; - if (vcConfig != null) { + if (config is VCConfiguration vcConfig) { vcProj = vcConfig.project as VCProject; configName = vcConfig.ConfigurationName; - var vcPlatform = vcConfig.Platform as VCPlatform; - if (vcPlatform != null) + if (vcConfig.Platform is VCPlatform vcPlatform) platformName = vcPlatform.Name; try { expanded = vcConfig.Evaluate(expanded); @@ -1475,11 +1469,9 @@ namespace QtVsTools.Core vcFile = vcFileConfig.File as VCFile; if (vcFile != null) vcProj = vcFile.project as VCProject; - var vcProjConfig = vcFileConfig.ProjectConfiguration as VCConfiguration; - if (vcProjConfig != null) { + if (vcFileConfig.ProjectConfiguration is VCConfiguration vcProjConfig) { configName = vcProjConfig.ConfigurationName; - var vcPlatform = vcProjConfig.Platform as VCPlatform; - if (vcPlatform != null) + if (vcProjConfig.Platform is VCPlatform vcPlatform) platformName = vcPlatform.Name; } try { diff --git a/QtVsTools.Core/MsBuildProject.cs b/QtVsTools.Core/MsBuildProject.cs index 533acb16..2efdad5a 100644 --- a/QtVsTools.Core/MsBuildProject.cs +++ b/QtVsTools.Core/MsBuildProject.cs @@ -961,8 +961,7 @@ namespace QtVsTools.Core evaluator.Properties.Add(configProp.Name.LocalName, (string)configProp); if (!qtMsBuild.SetCommandLine(itemType, item, commandLine, evaluator)) { int lineNumber = 1; - var errorLine = row.command as IXmlLineInfo; - if (errorLine != null && errorLine.HasLineInfo()) + if (row.command is IXmlLineInfo errorLine && errorLine.HasLineInfo()) lineNumber = errorLine.LineNumber; Messages.Print(string.Format( diff --git a/QtVsTools.Core/ProjectExporter.cs b/QtVsTools.Core/ProjectExporter.cs index 757f1f3a..ff737eea 100644 --- a/QtVsTools.Core/ProjectExporter.cs +++ b/QtVsTools.Core/ProjectExporter.cs @@ -199,8 +199,7 @@ namespace QtVsTools.Core if (config.ConfigurationType == ConfigurationTypes.typeStaticLibrary) option.List.Add("staticlib"); if (linker != null) { - var linkerRule = linker as IVCRulePropertyStorage; - var generateDebugInformation = (linkerRule != null) ? + var generateDebugInformation = (linker is IVCRulePropertyStorage linkerRule) ? linkerRule.GetUnevaluatedPropertyValue("GenerateDebugInformation") : null; if (generateDebugInformation != "false") option.List.Add("debug"); @@ -448,10 +447,7 @@ namespace QtVsTools.Core if (!d.StartsWith("$(qtdir)\\include", StringComparison.OrdinalIgnoreCase) && !d.StartsWith(qtDir + "\\include", StringComparison.OrdinalIgnoreCase) && !d.EndsWith("win32-msvc2005", StringComparison.OrdinalIgnoreCase)) { - - var vcConfig = project.ConfigurationManager.ActiveConfiguration.Object - as VCConfiguration; - if (vcConfig != null) + if (project.ConfigurationManager.ActiveConfiguration.Object is VCConfiguration vcConfig) HelperFunctions.ExpandString(ref d, vcConfig); if (HelperFunctions.IsAbsoluteFilePath(d)) d = HelperFunctions.GetRelativePath(project.FullName, d); diff --git a/QtVsTools.Core/QtProject.cs b/QtVsTools.Core/QtProject.cs index 5965635e..51e649c1 100644 --- a/QtVsTools.Core/QtProject.cs +++ b/QtVsTools.Core/QtProject.cs @@ -558,8 +558,7 @@ namespace QtVsTools.Core string description, string outputFile) { - var file = config.File as VCFile; - if (file != null) + if (config.File is VCFile file) file.ItemType = QtUic.ItemTypeName; qtMsBuild.SetItemProperty(config, QtUic.Property.ExecutionDescription, description); qtMsBuild.SetItemProperty(config, QtUic.Property.OutputFile, outputFile); @@ -684,15 +683,15 @@ namespace QtVsTools.Core public string GetDefines(VCFileConfiguration conf) { var defines = string.Empty; - var propsFile = conf.Tool as IVCRulePropertyStorage; - var projectConfig = conf.ProjectConfiguration as VCConfiguration; - var propsProject = projectConfig.Rules.Item("CL") as IVCRulePropertyStorage; - if (propsFile != null) { + if (conf.Tool is IVCRulePropertyStorage propsFile) { try { defines = propsFile.GetUnevaluatedPropertyValue("PreprocessorDefinitions"); } catch { } } - if (string.IsNullOrEmpty(defines) && propsProject != null) { + + var projectConfig = conf.ProjectConfiguration as VCConfiguration; + if (string.IsNullOrEmpty(defines) + && projectConfig?.Rules.Item("CL") is IVCRulePropertyStorage propsProject) { try { defines = propsProject.GetUnevaluatedPropertyValue("PreprocessorDefinitions"); } catch { } @@ -734,8 +733,7 @@ namespace QtVsTools.Core var projectConfig = conf.ProjectConfiguration as VCConfiguration; includeList.AddRange(GetIncludesFromCompilerTool(CompilerToolWrapper.Create(projectConfig))); - var propertySheets = projectConfig.PropertySheets as IVCCollection; - if (propertySheets != null) { + if (projectConfig.PropertySheets is IVCCollection propertySheets) { foreach (VCPropertySheet sheet in propertySheets) includeList.AddRange(GetIncludesFromPropertySheet(sheet)); } @@ -756,8 +754,7 @@ namespace QtVsTools.Core private List<string> GetIncludesFromPropertySheet(VCPropertySheet sheet) { var includeList = GetIncludesFromCompilerTool(CompilerToolWrapper.Create(sheet)); - var propertySheets = sheet.PropertySheets as IVCCollection; - if (propertySheets != null) { + if (sheet.PropertySheets is IVCCollection propertySheets) { foreach (VCPropertySheet subSheet in propertySheets) includeList.AddRange(GetIncludesFromPropertySheet(subSheet)); } @@ -1225,8 +1222,7 @@ namespace QtVsTools.Core File.WriteAllText(cbtFullPath, string.Format( "This is a dummy file needed to create {0}", mocFileName)); file = AddFileInSubfilter(Filters.GeneratedFiles(), null, cbtFullPath, true); - var mocFileItem = file.Object as ProjectItem; - if (mocFileItem != null) + if (file.Object is ProjectItem mocFileItem) HelperFunctions.EnsureCustomBuildToolAvailable(mocFileItem); } @@ -1381,8 +1377,7 @@ namespace QtVsTools.Core string nameOnly, string qrcCppFile) { - var file = vfc.File as VCFile; - if (file != null) + if (vfc.File is VCFile file) file.ItemType = QtRcc.ItemTypeName; qtMsBuild.SetItemProperty(vfc, QtRcc.Property.ExecutionDescription, "Rcc'ing " + ProjectMacros.FileName + "..."); @@ -1559,8 +1554,7 @@ namespace QtVsTools.Core List<VCFile> GetCppMocFiles(VCFile cppFile) { List<VCFile> mocFiles = new List<VCFile>(); - var vcProj = cppFile.project as VCProject; - if (vcProj != null) { + if (cppFile.project is VCProject vcProj) { mocFiles.AddRange(from VCFile vcFile in (IVCCollection)vcProj.Files where vcFile.ItemType == "CustomBuild" @@ -1589,8 +1583,7 @@ namespace QtVsTools.Core if (!IsQtMsBuildEnabled()) return File.Exists(Path.ChangeExtension(cppFile.FullPath, ".cbt")); - var vcProj = cppFile.project as VCProject; - if (vcProj != null) { + if (cppFile.project is VCProject vcProj) { foreach (VCFile vcFile in (IVCCollection)vcProj.Files) { if (vcFile.ItemType == "CustomBuild") { if (IsCppMocFileCustomBuild(vcProj, vcFile, cppFile)) @@ -3252,8 +3245,7 @@ namespace QtVsTools.Core debuggerEnv = propertyAccess.GetPropertyValue( "LocalDebuggerEnvironment", cur_solution, "UserFile"); if (!string.IsNullOrEmpty(debuggerEnv)) { - var debugSettings = conf.DebugSettings as VCDebugSettings; - if (debugSettings != null) { + if (conf.DebugSettings is VCDebugSettings debugSettings) { //Get original value without expanded properties debuggerEnv = debugSettings.Environment; } @@ -3632,10 +3624,10 @@ namespace QtVsTools.Core { if (propertyStorage == null) return null; - if (propertyStorage is VCFileConfiguration) - return GetParentProject(propertyStorage as VCFileConfiguration); - else if (propertyStorage is VCConfiguration) - return GetParentProject(propertyStorage as VCConfiguration); + if (propertyStorage is VCFileConfiguration configuration) + return GetParentProject(configuration); + else if (propertyStorage is VCConfiguration storage) + return GetParentProject(storage); return null; } diff --git a/QtVsTools.Package/Common/Json/Serializable.cs b/QtVsTools.Package/Common/Json/Serializable.cs index c843c279..c9b521fb 100644 --- a/QtVsTools.Package/Common/Json/Serializable.cs +++ b/QtVsTools.Package/Common/Json/Serializable.cs @@ -192,9 +192,11 @@ namespace QtVsTools.Json var container = toDo.Dequeue(); foreach (var defObj in container.PendingObjects) { defObj.Deserialize(); - var subContainer = defObj.Object as IDeferredObjectContainer; - if (subContainer != null && subContainer.PendingObjects.Any()) + if (defObj.Object is IDeferredObjectContainer subContainer + && subContainer.PendingObjects.Any()) { toDo.Enqueue(subContainer); + + } } } return obj; diff --git a/QtVsTools.Package/Common/Json/Serializer.cs b/QtVsTools.Package/Common/Json/Serializer.cs index 997b92e3..14c49933 100644 --- a/QtVsTools.Package/Common/Json/Serializer.cs +++ b/QtVsTools.Package/Common/Json/Serializer.cs @@ -151,8 +151,7 @@ namespace QtVsTools.Json using (reader = XmlReader.Create(data.XmlStream)) { var obj = serializer.ReadObject(reader, false); - var container = obj as IDeferredObjectContainer; - if (container != null) + if (obj is IDeferredObjectContainer container) deferredObjects.ForEach(x => container.Add(x)); return obj; diff --git a/QtVsTools.Package/Options/QtOptionsPage.cs b/QtVsTools.Package/Options/QtOptionsPage.cs index 9f339e4f..c5e529b4 100644 --- a/QtVsTools.Package/Options/QtOptionsPage.cs +++ b/QtVsTools.Package/Options/QtOptionsPage.cs @@ -144,8 +144,8 @@ namespace QtVsTools.Options object value, Type destinationType) { - if (value.GetType() == typeof(bool) && destinationType == typeof(string)) - return ((bool)value) ? "Enable" : "Disable"; + if (value is bool b && destinationType == typeof(string)) + return b ? "Enable" : "Disable"; return base.ConvertTo(context, culture, value, destinationType); } } diff --git a/QtVsTools.Package/Package/DteEventsHandler.cs b/QtVsTools.Package/Package/DteEventsHandler.cs index ca6222a2..3476481a 100644 --- a/QtVsTools.Package/Package/DteEventsHandler.cs +++ b/QtVsTools.Package/Package/DteEventsHandler.cs @@ -522,8 +522,7 @@ namespace QtVsTools return; // Retrieves the VCProjectEngine from the given project and registers the handlers for VCProjectEngineEvents. - var prjEngine = vcPrj.VCProjectEngine as VCProjectEngine; - if (prjEngine != null) { + if (vcPrj.VCProjectEngine is VCProjectEngine prjEngine) { vcProjectEngineEvents = prjEngine.Events as VCProjectEngineEvents; if (vcProjectEngineEvents != null) { try { @@ -616,8 +615,7 @@ namespace QtVsTools var pi = type.GetProperty(propertyName); if (pi != null) { foreach (Attribute attribute in pi.GetCustomAttributes(true)) { - var dispIdAttribute = attribute as DispIdAttribute; - if (dispIdAttribute != null) + if (attribute is DispIdAttribute dispIdAttribute) return dispIdAttribute.Value; } } diff --git a/QtVsTools.Package/QML/Debugging/AD7/QmlDebugAD7Engine.cs b/QtVsTools.Package/QML/Debugging/AD7/QmlDebugAD7Engine.cs index 03ca3f22..e4d5ed72 100644 --- a/QtVsTools.Package/QML/Debugging/AD7/QmlDebugAD7Engine.cs +++ b/QtVsTools.Package/QML/Debugging/AD7/QmlDebugAD7Engine.cs @@ -237,8 +237,7 @@ namespace QtVsTools.Qml.Debug.AD7 int IDebugEngine2.ContinueFromSynchronousEvent(IDebugEvent2 pEvent) { - var evtProgramDestroy = pEvent as ProgramDestroyEvent; - if (evtProgramDestroy != null) + if (pEvent is ProgramDestroyEvent evtProgramDestroy) evtProgramDestroy.Program.Dispose(); return VSConstants.S_OK; diff --git a/QtVsTools.Package/QML/Debugging/AD7/QmlDebugAD7InfoHelpers.cs b/QtVsTools.Package/QML/Debugging/AD7/QmlDebugAD7InfoHelpers.cs index 1994bf36..48f2eb93 100644 --- a/QtVsTools.Package/QML/Debugging/AD7/QmlDebugAD7InfoHelpers.cs +++ b/QtVsTools.Package/QML/Debugging/AD7/QmlDebugAD7InfoHelpers.cs @@ -185,8 +185,7 @@ namespace QtVsTools.Qml.Debug.AD7 TFieldMask fieldMask, out TStruct infoStruct) { - var mappingToStruct = mapping as Mapping<TStruct, TFieldMask>; - if (mappingToStruct != null) + if (mapping is Mapping<TStruct, TFieldMask> mappingToStruct) mappingToStruct.Map(this as TDerived, fieldMask, out infoStruct); else infoStruct = default(TStruct); diff --git a/QtVsTools.Package/QtMsBuild/QtModulesPopup.xaml.cs b/QtVsTools.Package/QtMsBuild/QtModulesPopup.xaml.cs index 04edd82e..463c85c9 100644 --- a/QtVsTools.Package/QtMsBuild/QtModulesPopup.xaml.cs +++ b/QtVsTools.Package/QtMsBuild/QtModulesPopup.xaml.cs @@ -81,8 +81,7 @@ namespace QtVsTools.QtMsBuild private void PopupListBox_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter || e.Key == Key.Space) { - var module = PopupListBox.SelectedItem as Module; - if (module != null && module.IsEnabled) + if (PopupListBox.SelectedItem is Module module && module.IsEnabled) module.CheckBox.IsChecked = (module.CheckBox.IsChecked != true); } } diff --git a/QtVsTools.Package/VisualStudio/VsShell.cs b/QtVsTools.Package/VisualStudio/VsShell.cs index 7d7170da..a4c9a9bc 100644 --- a/QtVsTools.Package/VisualStudio/VsShell.cs +++ b/QtVsTools.Package/VisualStudio/VsShell.cs @@ -58,8 +58,8 @@ namespace QtVsTools.VisualStudio object objProp; int res = vsShell.GetProperty((int)__VSSPROPID2.VSSPROPID_InstallRootDir, out objProp); - if (res == VSConstants.S_OK && objProp is string) - _InstallRootDir = objProp as string; + if (res == VSConstants.S_OK && objProp is string property) + _InstallRootDir = property; } public static EnvDTE.Project GetProject(IVsHierarchy context) @@ -69,10 +69,9 @@ namespace QtVsTools.VisualStudio object value; int res = context.GetProperty( (uint)VSConstants.VSITEMID.Root, (int)__VSHPROPID.VSHPROPID_ExtObject, out value); - if (res != VSConstants.S_OK) - return null; - - return value as EnvDTE.Project; + if (res == VSConstants.S_OK && value is EnvDTE.Project project) + return project; + return null; } public static EnvDTE.ProjectItem GetProjectItem(IVsHierarchy context, uint itemid) @@ -82,10 +81,9 @@ namespace QtVsTools.VisualStudio object value; int res = context.GetProperty( itemid, (int)__VSHPROPID.VSHPROPID_ExtObject, out value); - if (res != VSConstants.S_OK) - return null; - - return value as EnvDTE.ProjectItem; + if (res == VSConstants.S_OK && value is EnvDTE.ProjectItem item) + return item; + return null; } public static EnvDTE.Document GetDocument(IVsHierarchy context, uint itemid) |