From 35ea2f6f1c41ab8d1c9d8e377b334015b40e1ef5 Mon Sep 17 00:00:00 2001 From: Dan Fleming Date: Wed, 3 May 2017 13:00:56 -0400 Subject: [PATCH 1/8] ignore infer output directory --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 0122493b..93f60c47 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,9 @@ Carthage/Build fastlane/report.xml fastlane/screenshots +# Infer +infer-out + # Documentation documentation/output/ From 14efb7452d8f69a61b182e08ff43c60dbda626c8 Mon Sep 17 00:00:00 2001 From: Dan Fleming Date: Wed, 3 May 2017 13:35:32 -0400 Subject: [PATCH 2/8] oclint: collapse nested if statements --- sources/HUBCollectionView.m | 10 ++++------ sources/HUBComponentWrapper.m | 10 ++++------ .../HUBViewControllerExperimentalImplementation.m | 13 +++++-------- sources/HUBViewControllerImplementation.m | 13 +++++-------- sources/HUBViewModelLoaderImplementation.m | 12 +++++------- 5 files changed, 23 insertions(+), 35 deletions(-) diff --git a/sources/HUBCollectionView.m b/sources/HUBCollectionView.m index f23dbee3..590b23cd 100644 --- a/sources/HUBCollectionView.m +++ b/sources/HUBCollectionView.m @@ -35,12 +35,10 @@ - (void)setContentOffset:(CGPoint)contentOffset { id const delegate = self.delegate; - if (delegate != nil) { - if (![delegate collectionViewShouldBeginScrolling:self]) { - self.panGestureRecognizer.enabled = NO; - self.panGestureRecognizer.enabled = YES; - return; - } + if (delegate != nil && ![delegate collectionViewShouldBeginScrolling:self]) { + self.panGestureRecognizer.enabled = NO; + self.panGestureRecognizer.enabled = YES; + return; } [super setContentOffset:contentOffset]; diff --git a/sources/HUBComponentWrapper.m b/sources/HUBComponentWrapper.m index 203b39ae..5e4f39d9 100644 --- a/sources/HUBComponentWrapper.m +++ b/sources/HUBComponentWrapper.m @@ -186,12 +186,10 @@ - (void)loadView BOOL const viewLoaded = (self.view != nil); UIView * const view = HUBComponentLoadViewIfNeeded(self.component); - if (!viewLoaded) { - if (HUBConformsToProtocol(self.component, @protocol(HUBComponentViewObserver))) { - HUBComponentResizeObservingView * const resizeObservingView = [[HUBComponentResizeObservingView alloc] initWithFrame:view.bounds]; - resizeObservingView.delegate = self; - [view addSubview:resizeObservingView]; - } + if (!viewLoaded && HUBConformsToProtocol(self.component, @protocol(HUBComponentViewObserver))) { + HUBComponentResizeObservingView * const resizeObservingView = [[HUBComponentResizeObservingView alloc] initWithFrame:view.bounds]; + resizeObservingView.delegate = self; + [view addSubview:resizeObservingView]; } } diff --git a/sources/HUBViewControllerExperimentalImplementation.m b/sources/HUBViewControllerExperimentalImplementation.m index b8d4b260..8bddb0fd 100644 --- a/sources/HUBViewControllerExperimentalImplementation.m +++ b/sources/HUBViewControllerExperimentalImplementation.m @@ -768,10 +768,9 @@ - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView currentContentOffset:scrollView.contentOffset proposedContentOffset:*targetContentOffset]; - if (targetContentOffset->y >= (scrollView.contentSize.height - CGRectGetHeight(scrollView.frame))) { - if (!self.viewModelLoader.isLoading) { - [self.viewModelLoader loadNextPageForCurrentViewModel]; - } + CGFloat threshold = scrollView.contentSize.height - CGRectGetHeight(scrollView.frame); + if (targetContentOffset->y >= threshold && !self.viewModelLoader.isLoading) { + [self.viewModelLoader loadNextPageForCurrentViewModel]; } } @@ -1138,10 +1137,8 @@ - (void)collectionViewCellWillAppear:(HUBComponentCollectionViewCell *)cell return; } - if (wrapper.viewHasAppearedSinceLastModelChange) { - if (!ignorePreviousAppearance) { - return; - } + if (wrapper.viewHasAppearedSinceLastModelChange && !ignorePreviousAppearance) { + return; } [self componentWrapperWillAppear:wrapper]; diff --git a/sources/HUBViewControllerImplementation.m b/sources/HUBViewControllerImplementation.m index 871f2471..2e6a9851 100644 --- a/sources/HUBViewControllerImplementation.m +++ b/sources/HUBViewControllerImplementation.m @@ -779,10 +779,9 @@ - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView currentContentOffset:scrollView.contentOffset proposedContentOffset:*targetContentOffset]; - if (targetContentOffset->y >= (scrollView.contentSize.height - CGRectGetHeight(scrollView.frame))) { - if (!self.viewModelLoader.isLoading) { - [self.viewModelLoader loadNextPageForCurrentViewModel]; - } + CGFloat threshold = scrollView.contentSize.height - CGRectGetHeight(scrollView.frame); + if (targetContentOffset->y >= threshold && !self.viewModelLoader.isLoading) { + [self.viewModelLoader loadNextPageForCurrentViewModel]; } } @@ -1103,10 +1102,8 @@ - (void)collectionViewCellWillAppear:(HUBComponentCollectionViewCell *)cell return; } - if (wrapper.viewHasAppearedSinceLastModelChange) { - if (!ignorePreviousAppearance) { - return; - } + if (wrapper.viewHasAppearedSinceLastModelChange && !ignorePreviousAppearance) { + return; } [self componentWrapperWillAppear:wrapper]; diff --git a/sources/HUBViewModelLoaderImplementation.m b/sources/HUBViewModelLoaderImplementation.m index 0fe3cd3c..6cf67cc3 100644 --- a/sources/HUBViewModelLoaderImplementation.m +++ b/sources/HUBViewModelLoaderImplementation.m @@ -179,13 +179,11 @@ - (void)loadViewModel [self connectivityStateResolverStateDidChange:self.connectivityStateResolver]; [self.connectivityStateResolver addObserver:self]; - if (self.contentReloadPolicy != nil) { - if (self.previouslyLoadedViewModel != nil) { - id const previouslyLoadedViewModel = self.previouslyLoadedViewModel; - - if (![self.contentReloadPolicy shouldReloadContentForViewURI:self.viewURI currentViewModel:previouslyLoadedViewModel]) { - return; - } + if (self.contentReloadPolicy != nil && self.previouslyLoadedViewModel != nil) { + id const previouslyLoadedViewModel = self.previouslyLoadedViewModel; + + if (![self.contentReloadPolicy shouldReloadContentForViewURI:self.viewURI currentViewModel:previouslyLoadedViewModel]) { + return; } } From 3484c79d7fddb51be299883d8887ef425b5283e5 Mon Sep 17 00:00:00 2001 From: Dan Fleming Date: Wed, 3 May 2017 13:36:30 -0400 Subject: [PATCH 3/8] oclint: add void to parameterless blocks --- sources/HUBViewControllerExperimentalImplementation.m | 6 +++--- sources/HUBViewControllerImplementation.m | 4 ++-- sources/HUBViewModelRenderer.m | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/sources/HUBViewControllerExperimentalImplementation.m b/sources/HUBViewControllerExperimentalImplementation.m index 8bddb0fd..6c47dde8 100644 --- a/sources/HUBViewControllerExperimentalImplementation.m +++ b/sources/HUBViewControllerExperimentalImplementation.m @@ -841,7 +841,7 @@ - (void)renderViewModel:(id)viewModel completion:(void (^)(void))completionBlock { __weak __typeof(self) weakSelf = self; - void (^renderBlock)() = ^{ + void (^renderBlock)(void) = ^{ __strong __typeof(self) strongSelf = weakSelf; [strongSelf renderViewModel:viewModel addHeaderMargin:addHeaderMargin @@ -1252,7 +1252,7 @@ - (void)removeComponentWrapperFromLookupTables:(nullable HUBComponentWrapper *)c - (void)scrollToRootBodyComponentAtIndex:(NSUInteger)componentIndex scrollPosition:(HUBScrollPosition)scrollPosition animated:(BOOL)animated - completion:(void (^)())completion + completion:(void (^)(void))completion { NSParameterAssert(componentIndex <= (NSUInteger)[self.collectionView numberOfItemsInSection:0]); @@ -1295,7 +1295,7 @@ - (void)scrollToRemainingComponentsOfType:(HUBComponentType)componentType } __weak HUBViewControllerExperimentalImplementation *weakSelf = self; - void (^stepCompletionHandler)() = ^{ + void (^stepCompletionHandler)(void) = ^{ HUBViewControllerExperimentalImplementation *strongSelf = weakSelf; HUBComponentWrapper *childComponentWrapper = nil; diff --git a/sources/HUBViewControllerImplementation.m b/sources/HUBViewControllerImplementation.m index 2e6a9851..fd00abaa 100644 --- a/sources/HUBViewControllerImplementation.m +++ b/sources/HUBViewControllerImplementation.m @@ -1217,7 +1217,7 @@ - (void)removeComponentWrapperFromLookupTables:(nullable HUBComponentWrapper *)c - (void)scrollToRootBodyComponentAtIndex:(NSUInteger)componentIndex scrollPosition:(HUBScrollPosition)scrollPosition animated:(BOOL)animated - completion:(void (^)())completion + completion:(void (^)(void))completion { NSParameterAssert(componentIndex <= (NSUInteger)[self.collectionView numberOfItemsInSection:0]); @@ -1260,7 +1260,7 @@ - (void)scrollToRemainingComponentsOfType:(HUBComponentType)componentType } __weak HUBViewControllerImplementation *weakSelf = self; - void (^stepCompletionHandler)() = ^{ + void (^stepCompletionHandler)(void) = ^{ HUBViewControllerImplementation *strongSelf = weakSelf; HUBComponentWrapper *childComponentWrapper = nil; diff --git a/sources/HUBViewModelRenderer.m b/sources/HUBViewModelRenderer.m index bccb43b2..d3d85b4b 100644 --- a/sources/HUBViewModelRenderer.m +++ b/sources/HUBViewModelRenderer.m @@ -41,7 +41,7 @@ - (void)renderViewModel:(id)viewModel completion:(void (^)(void))completionBlock { __weak __typeof(self) weakSelf = self; - void (^renderBlock)() = ^{ + void (^renderBlock)(void) = ^{ __strong __typeof(self) strongSelf = weakSelf; [strongSelf renderViewModel:viewModel inCollectionView:collectionView @@ -77,7 +77,7 @@ - (void)renderViewModel:(id)viewModel synchronously, or called from either of of the collection view's performBatchUpdates:completion: blocks), I've tried to separate that logic out into 2 block methods: layoutBlock and postLayoutBlock. */ - void (^layoutBlock)() = ^{ + void (^layoutBlock)(void) = ^{ [layout computeForCollectionViewSize:collectionView.frame.size viewModel:viewModel diff:diff @@ -85,7 +85,7 @@ - (void)renderViewModel:(id)viewModel }; __weak __typeof(self) weakSelf = self; - void (^postLayoutBlock)() = ^{ + void (^postLayoutBlock)(void) = ^{ __strong __typeof(self) strongSelf = weakSelf; strongSelf.lastRenderedViewModel = viewModel; completionBlock(); From 696d753c9f9513f7cfeeecf35a60b252416d40b1 Mon Sep 17 00:00:00 2001 From: Dan Fleming Date: Wed, 3 May 2017 13:36:42 -0400 Subject: [PATCH 4/8] oclint: dead code warning --- sources/HUBLiveServiceFactory.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sources/HUBLiveServiceFactory.m b/sources/HUBLiveServiceFactory.m index 7751241e..71c2e365 100644 --- a/sources/HUBLiveServiceFactory.m +++ b/sources/HUBLiveServiceFactory.m @@ -29,9 +29,9 @@ - (id)createLiveService { #if HUB_DEBUG return [HUBLiveServiceImplementation new]; -#endif - +#else return nil; +#endif } @end From 9e34cd064c62ba64fde1bbd0ef49616bfbb343ac Mon Sep 17 00:00:00 2001 From: Dan Fleming Date: Wed, 3 May 2017 13:37:16 -0400 Subject: [PATCH 5/8] oclint: use subscripts (don't complain about nil) --- sources/HUBComponentModelBuilderImplementation.m | 2 +- sources/HUBViewModelBuilderImplementation.m | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sources/HUBComponentModelBuilderImplementation.m b/sources/HUBComponentModelBuilderImplementation.m index ad61a74a..6fb02ca5 100644 --- a/sources/HUBComponentModelBuilderImplementation.m +++ b/sources/HUBComponentModelBuilderImplementation.m @@ -507,7 +507,7 @@ - (id)copyWithZone:(nullable NSZone *)zone id const imageData = [builder buildWithIdentifier:imageIdentifier type:HUBComponentImageTypeCustom]; if (imageData != nil) { - [customImageData setObject:imageData forKey:imageIdentifier]; + customImageData[imageIdentifier] = imageData; } } diff --git a/sources/HUBViewModelBuilderImplementation.m b/sources/HUBViewModelBuilderImplementation.m index d2a9a067..5d35fccd 100644 --- a/sources/HUBViewModelBuilderImplementation.m +++ b/sources/HUBViewModelBuilderImplementation.m @@ -275,7 +275,7 @@ - (void)setCustomDataValue:(nullable id)value forKey:(nonnull NSString *)key if (value == nil) { [customData removeObjectForKey:key]; } else { - [customData setObject:(id)value forKey:key]; + customData[key] = value; } self.customData = customData; From 8b7f1cdc032ee78bcbab1d0c8244b5082c344b39 Mon Sep 17 00:00:00 2001 From: Dan Fleming Date: Wed, 3 May 2017 13:37:46 -0400 Subject: [PATCH 6/8] oclint: remove unnecessary else statements --- sources/HUBDefaultComponentLayoutManager.m | 18 +++++++++--------- sources/HUBIconImplementation.m | 3 +-- .../HUBViewControllerFactoryImplementation.m | 3 +-- sources/HUBViewModelDiff.m | 18 +++++++----------- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/sources/HUBDefaultComponentLayoutManager.m b/sources/HUBDefaultComponentLayoutManager.m index 8cc3308d..588d6340 100644 --- a/sources/HUBDefaultComponentLayoutManager.m +++ b/sources/HUBDefaultComponentLayoutManager.m @@ -82,16 +82,16 @@ - (CGFloat)horizontalMarginForComponentWithLayoutTraits:(NSSet fromViewModel, id toViewModel) { From 55e8d9ee9821840b05b2a9349d0b960070658b88 Mon Sep 17 00:00:00 2001 From: Dan Fleming Date: Wed, 3 May 2017 13:38:02 -0400 Subject: [PATCH 7/8] oclint: remove unnecessary variables --- sources/HUBDefaultConnectivityStateResolver.m | 5 +---- sources/HUBViewControllerExperimentalImplementation.m | 3 +-- sources/HUBViewControllerImplementation.m | 3 +-- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/sources/HUBDefaultConnectivityStateResolver.m b/sources/HUBDefaultConnectivityStateResolver.m index 10488cfa..43264821 100644 --- a/sources/HUBDefaultConnectivityStateResolver.m +++ b/sources/HUBDefaultConnectivityStateResolver.m @@ -115,13 +115,10 @@ - (void)startObservingReachability - (SCNetworkReachabilityContext)createReachabilityContext { __weak __typeof(self) weakSelf = self; - - SCNetworkReachabilityContext const context = { + return (SCNetworkReachabilityContext){ .version = 0, .info = (__bridge void *)weakSelf }; - - return context; } - (HUBConnectivityState)connectivityStateFromReachabilityFlags:(SCNetworkReachabilityFlags)flags diff --git a/sources/HUBViewControllerExperimentalImplementation.m b/sources/HUBViewControllerExperimentalImplementation.m index 6c47dde8..2a43d21c 100644 --- a/sources/HUBViewControllerExperimentalImplementation.m +++ b/sources/HUBViewControllerExperimentalImplementation.m @@ -973,8 +973,7 @@ - (CGFloat)calculateTopContentInset CGFloat const statusBarHeight = CGRectGetHeight([UIApplication sharedApplication].statusBarFrame); CGFloat const navigationBarWidth = CGRectGetWidth(self.navigationController.navigationBar.frame); CGFloat const navigationBarHeight = CGRectGetHeight(self.navigationController.navigationBar.frame); - CGFloat const topBarHeight = MIN(statusBarWidth, statusBarHeight) + MIN(navigationBarWidth, navigationBarHeight); - return topBarHeight; + return MIN(statusBarWidth, statusBarHeight) + MIN(navigationBarWidth, navigationBarHeight); } - (BOOL)shouldAutomaticallyManageTopContentInset diff --git a/sources/HUBViewControllerImplementation.m b/sources/HUBViewControllerImplementation.m index fd00abaa..16306ba3 100644 --- a/sources/HUBViewControllerImplementation.m +++ b/sources/HUBViewControllerImplementation.m @@ -938,8 +938,7 @@ - (CGFloat)calculateTopContentInset CGFloat const statusBarHeight = CGRectGetHeight([UIApplication sharedApplication].statusBarFrame); CGFloat const navigationBarWidth = CGRectGetWidth(self.navigationController.navigationBar.frame); CGFloat const navigationBarHeight = CGRectGetHeight(self.navigationController.navigationBar.frame); - CGFloat const topBarHeight = MIN(statusBarWidth, statusBarHeight) + MIN(navigationBarWidth, navigationBarHeight); - return topBarHeight; + return MIN(statusBarWidth, statusBarHeight) + MIN(navigationBarWidth, navigationBarHeight); } - (BOOL)shouldAutomaticallyManageTopContentInset From 56404ca3c592ff3d03cc2f59396dcea855af95ab Mon Sep 17 00:00:00 2001 From: Dan Fleming Date: Wed, 3 May 2017 13:44:41 -0400 Subject: [PATCH 8/8] oclint: prefer early exit --- sources/HUBAutoEquatable.m | 47 ++++++++++++++------------- sources/HUBViewModelDiff.m | 66 ++++++++++++++++++++------------------ 2 files changed, 58 insertions(+), 55 deletions(-) diff --git a/sources/HUBAutoEquatable.m b/sources/HUBAutoEquatable.m index 3d09a7fd..9784972a 100644 --- a/sources/HUBAutoEquatable.m +++ b/sources/HUBAutoEquatable.m @@ -73,35 +73,36 @@ - (HUBAutoEquatableComparisonMap *)getOrCreateComparisonMap HUBAutoEquatableComparisonMap *comparisonMap = comparisonMapsForClassNames[className]; - if (comparisonMap == nil) { - NSSet * const ignoredPropertyNames = [[self class] ignoredAutoEquatablePropertyNames]; - HUBAutoEquatableMutableComparisonMap * const mutableComparisonMap = [HUBAutoEquatableMutableComparisonMap new]; + if (comparisonMap != nil) { + return comparisonMap; + } + + NSSet * const ignoredPropertyNames = [[self class] ignoredAutoEquatablePropertyNames]; + HUBAutoEquatableMutableComparisonMap * const mutableComparisonMap = [HUBAutoEquatableMutableComparisonMap new]; + + unsigned int propertyCount; + const objc_property_t * propertyList = class_copyPropertyList([self class], &propertyCount); + + for (unsigned int i = 0; i < propertyCount; i++) { + const objc_property_t property = propertyList[i]; + const char * propertyNameCString = property_getName(property); + NSString * const propertyName = [NSString stringWithUTF8String:propertyNameCString]; - unsigned int propertyCount; - const objc_property_t * propertyList = class_copyPropertyList([self class], &propertyCount); + if (protocol_getProperty(@protocol(NSObject), propertyNameCString, YES, YES) != NULL) { + continue; + } - for (unsigned int i = 0; i < propertyCount; i++) { - const objc_property_t property = propertyList[i]; - const char * propertyNameCString = property_getName(property); - NSString * const propertyName = [NSString stringWithUTF8String:propertyNameCString]; - - if (protocol_getProperty(@protocol(NSObject), propertyNameCString, YES, YES) != NULL) { - continue; - } - - if ([ignoredPropertyNames containsObject:propertyName]) { - continue; - } - - mutableComparisonMap[propertyName] = ^(NSObject * const objectA, NSObject * const objectB) { - return HUBPropertyIsEqual(objectA, objectB, propertyName); - }; + if ([ignoredPropertyNames containsObject:propertyName]) { + continue; } - comparisonMap = mutableComparisonMap; - comparisonMapsForClassNames[className] = comparisonMap; + mutableComparisonMap[propertyName] = ^(NSObject * const objectA, NSObject * const objectB) { + return HUBPropertyIsEqual(objectA, objectB, propertyName); + }; } + comparisonMap = mutableComparisonMap; + comparisonMapsForClassNames[className] = comparisonMap; return comparisonMap; } diff --git a/sources/HUBViewModelDiff.m b/sources/HUBViewModelDiff.m index 49a8206d..36022897 100644 --- a/sources/HUBViewModelDiff.m +++ b/sources/HUBViewModelDiff.m @@ -349,42 +349,44 @@ - (HUBDiffStepType)type } /// Here the goal is to follow the diagonal line with additional steps to find the longest common sequence - if (step.to.x <= fromCount && step.to.y <= toCount) { - [steps addObject:step]; - - NSInteger x = step.to.x; - NSInteger y = step.to.y; - - while (x >= 0 && y >= 0 && x < fromCount && y < toCount) { - id target = toViewModel.bodyComponentModels[(NSUInteger)y]; - id base = fromViewModel.bodyComponentModels[(NSUInteger)x]; - - /** - * Only the element's identity is compared here, as equality is checked later in order to determine - * the location of updates. - */ - if ([base.identifier isEqual:target.identifier]) { - // A match is found and another step can be taken diagonally. - x += 1; - y += 1; - - HUBDiffStep *nextStep = [[HUBDiffStep alloc] initWithFromPoint:HUBDiffPointMake(x - 1, y - 1) toPoint:HUBDiffPointMake(x, y)]; - - [steps addObject:nextStep]; - } else { - break; - } - } + if (step.to.x > fromCount || step.to.y > toCount) { + continue; + } - // Only the x-point needs to be stored since y can be inferred with y = x - k - endpoints[index] = x; + [steps addObject:step]; - // The end of the graph has been reached, and a solution has been found. - if (x >= fromCount && y >= toCount) { - free(endpoints); - return steps; + NSInteger x = step.to.x; + NSInteger y = step.to.y; + + while (x >= 0 && y >= 0 && x < fromCount && y < toCount) { + id target = toViewModel.bodyComponentModels[(NSUInteger)y]; + id base = fromViewModel.bodyComponentModels[(NSUInteger)x]; + + /** + * Only the element's identity is compared here, as equality is checked later in order to determine + * the location of updates. + */ + if ([base.identifier isEqual:target.identifier]) { + // A match is found and another step can be taken diagonally. + x += 1; + y += 1; + + HUBDiffStep *nextStep = [[HUBDiffStep alloc] initWithFromPoint:HUBDiffPointMake(x - 1, y - 1) toPoint:HUBDiffPointMake(x, y)]; + + [steps addObject:nextStep]; + } else { + break; } } + + // Only the x-point needs to be stored since y can be inferred with y = x - k + endpoints[index] = x; + + // The end of the graph has been reached, and a solution has been found. + if (x >= fromCount && y >= toCount) { + free(endpoints); + return steps; + } } }