lol, this kills me every time I see it! You can just subset with the logical vector returned by is.finite()!
My students do this all the time, which() this and which() that... I understand why they do it, it makes much more sense as you read the code aloud, it's just so unnecessary!
But, yes, this is the correct way to solve this. If you wanted something a little more compact though, I rather quite like this little hack of the language,
set.seed(123)
x <- c(sample(4), NA, Inf, 0)
sum(x^2/x, na.rm = TRUE)
#> [1] 10
Alternately you could do,
sum(x - x + x, na.rm = TRUE)
#> [1] 10
You should not do this though, you should use @nirgrahamuk's solution, it's correct.
Created on 2020-09-20 by the reprex package (v0.3.0)