 <p>A very "dirty" solution would be to systematically check that the function's local environment is not changed after the "preamble". </p> <pre><code>fun &lt;- function(x) { a &lt;- x ls1 &lt;- ls() if (a&lt;0) { if (a &gt; -50) x &lt;- -1 else x &lt;- -2 } ls2 &lt;- ls() print(list(c(ls1,"ls1"),ls2)) if (!setequal(c(ls1,"ls1"), ls2)) stop("Something went terribly wrong!") return(x) } fun.typo &lt;- function(x) { a &lt;- x ls1 &lt;- ls() if (a&lt;0) { if (a &gt; -50) x &lt;- -1 else X &lt;- -2 } ls2 &lt;- ls() print(list(c(ls1,"ls1"),ls2)) if (!setequal(c(ls1,"ls1"), ls2)) stop("Something went terribly wrong!") return(x) } </code></pre> <p>With this "solution", <code>fun.typo(-60)</code> no longer silently gives a wrong answer...</p>