I have a homework problem that asks us to determine $\big[\,f(\hat{x}),\hat{p}\big]$ only by use of $$\big[\hat{x}^n,\hat{p}\big] = ni\hbar \hat{x}^{n-1}$$ as well as assuming $f(\hat{x})$ can be expanded into a Maclaurin series. My solution has me arrive at the correct value of the commutator given by the text, my question is if the individual steps make sense mathematically, in particular line 3.
Solution:
$$f(\hat{x}) = \sum \frac{f^{(n)}(0)\,\hat{x}^n}{n!}$$
$$[f(\hat{x}),\hat{p}] = \Bigg[\sum \frac{f^{(n)}(0)\,\hat{x}^n}{n!}\, ,\hat{p}\Bigg]$$
$$[f(\hat{x}),\hat{p}] = \sum \frac{f^{(n)}(0)}{n!}\big[\hat{x}^n,\hat{p}\big]$$
$$[f(\hat{x}),\hat{p}] = \sum \frac{f^{(n)}(0)}{n!}ni\hbar\hat{x}^{n-1}$$
$$[f(\hat{x}),\hat{p}] = i\hbar\sum \frac{f^{(n)}(0)}{(n-1)!}\hat{x}^{n-1}$$
Also,
$$\frac{d}{d\hat{x}}f(\hat{x}) = \frac{d}{d\hat{x}}\sum \frac{f^{(n)}(0)\,\hat{x}^n}{n!}$$
$$\frac{d}{d\hat{x}}f(\hat{x}) = \sum \frac{f^{(n)}(0)n\hat{x}^{n-1}}{n!}$$
$$\frac{d}{d\hat{x}}f(\hat{x}) = \sum \frac{f^{(n)}(0)\,\hat{x}^{n-1}}{(n-1)!}$$
Then,
$$[f(\hat{x}),\hat{p}] = i\hbar\frac{d}{d\hat{x}}f(\hat{x})$$
QED