]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/expect_fun_call.rs
Use source callsite in FormatArgsExpn::inputs_span
[rust.git] / tests / ui / expect_fun_call.rs
index cf764c43694a101c4a6f37e088df07b1cd695854..22e530b80349d81e27ef8fdeb015acbe54a027a5 100644 (file)
@@ -1,22 +1,23 @@
-// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
+// run-rustfix
 
 #![warn(clippy::expect_fun_call)]
-#![allow(clippy::useless_format)]
+#![allow(clippy::to_string_in_format_args)]
 
 /// Checks implementation of the `EXPECT_FUN_CALL` lint
 
-fn expect_fun_call() {
+macro_rules! one {
+    () => {
+        1
+    };
+}
+
+fn main() {
     struct Foo;
 
     impl Foo {
-        fn new() -> Self { Foo }
+        fn new() -> Self {
+            Foo
+        }
 
         fn expect(&self, msg: &str) {
             panic!("{}", msg)
@@ -36,6 +37,9 @@ fn expect(&self, msg: &str) {
     let with_none_and_as_str: Option<i32> = None;
     with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
 
+    let with_none_and_format_with_macro: Option<i32> = None;
+    with_none_and_format_with_macro.expect(format!("Error {}: fake error", one!()).as_str());
+
     let with_ok: Result<(), ()> = Ok(());
     with_ok.expect("error");
 
@@ -58,12 +62,43 @@ fn expect(&self, msg: &str) {
     let with_dummy_type_and_as_str = Foo::new();
     with_dummy_type_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
 
+    //Issue #2937
+    Some("foo").expect(format!("{} {}", 1, 2).as_ref());
+
     //Issue #2979 - this should not lint
-    let msg = "bar";
-    Some("foo").expect(msg);
+    {
+        let msg = "bar";
+        Some("foo").expect(msg);
+    }
 
-    Some("foo").expect({ &format!("error") });
-    Some("foo").expect(format!("error").as_ref());
-}
+    {
+        fn get_string() -> String {
+            "foo".to_string()
+        }
+
+        fn get_static_str() -> &'static str {
+            "foo"
+        }
+
+        fn get_non_static_str(_: &u32) -> &str {
+            "foo"
+        }
 
-fn main() {}
+        Some("foo").expect(&get_string());
+        Some("foo").expect(get_string().as_ref());
+        Some("foo").expect(get_string().as_str());
+
+        Some("foo").expect(get_static_str());
+        Some("foo").expect(get_non_static_str(&0));
+    }
+
+    //Issue #3839
+    Some(true).expect(&format!("key {}, {}", 1, 2));
+
+    //Issue #4912 - the receiver is a &Option
+    {
+        let opt = Some(1);
+        let opt_ref = &opt;
+        opt_ref.expect(&format!("{:?}", opt_ref));
+    }
+}