Deuteronomy 4:3 (ESV)

3 Your eyes have seen what the LORD did at Baal-peor, for the LORD your God destroyed from among you all the men who followed the Baal of Peor.

The incident at Baal-peor (see Numbers 25:1–5) is recalled as a warning for Israel, that when they sin, they will be punished. The implication is then that by obeying the law, Israel would be spared punishment.