diff --git a/lib/sinatra/compat.rb b/lib/sinatra/compat.rb
index <HASH>..<HASH> 100644
--- a/lib/sinatra/compat.rb
+++ b/lib/sinatra/compat.rb
@@ -10,7 +10,9 @@ require 'sinatra/main'
 
 # Like Kernel#warn but outputs the location that triggered the warning.
 def sinatra_warn(*message)
-  line = caller[1].sub(/:in .*/, '')
+  line = caller.
+    detect { |line| line !~ /(?:lib\/sinatra\/|__DELEGATE__)/ }.
+    sub(/:in .*/, '')
   warn "#{line}: warning: #{message.join(' ')}"
 end
Better file/line detection in sinatra_warn