SA1118 False Positive?

Oct 12, 2011 at 9:38 PM

Hi,

Seems like I'm getting a false positive for SA1118 in the following code, or am I missing something?

return Observable.Using(
	() => new FileSystemWatcher(Path.GetDirectoryName(stream.Name), Path.GetFileName(stream.Name))
	{
		NotifyFilter = NotifyFilters.LastWrite
	},
	watcher => stream.ToObservable(  // Warning: SA1118
		bufferSize,
		watcher.Watch(WatcherChangeTypes.Created | WatcherChangeTypes.Changed)
			.StartWith((FileSystemNotification) null)));

Thanks,
Dave

Oct 12, 2011 at 9:43 PM

Hi,

Apparently StartWith is the problem.  It wants me to do this:

return Observable.Using(
	() => new FileSystemWatcher(Path.GetDirectoryName(stream.Name), Path.GetFileName(stream.Name))
	{
		NotifyFilter = NotifyFilters.LastWrite
	},
	watcher => stream.ToObservable(  // Warning: No warning
		bufferSize,
		watcher.Watch(WatcherChangeTypes.Created | WatcherChangeTypes.Changed).StartWith((FileSystemNotification) null)));

Which is strange, because the following code does not produce any warnings:

return Observable.Using(
	() => new StreamReader(stream, encoding),
	reader => Observable.Using(
		() => new FileSystemWatcher(Path.GetDirectoryName(stream.Name), Path.GetFileName(stream.Name))
		{
			NotifyFilter = NotifyFilters.LastWrite
		},
		watcher => reader.ToObservable(
			watcher.Watch(WatcherChangeTypes.Created | WatcherChangeTypes.Changed)
				.StartWith((FileSystemNotification) null),
			scheduler)));

Which is correct?  I'd prefer to keep StartWith on another line because it's much easier to read.

Thanks,
Dave