Skip to content

Commit c677838

Browse files
committed
Add str.as_str() for easy dereferencing of Box<str>
1 parent e9e13a6 commit c677838

File tree

6 files changed

+17
-62
lines changed

6 files changed

+17
-62
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -1721,20 +1721,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17211721
}
17221722
}
17231723

1724-
if item_name.name == sym::as_str && rcvr_ty.peel_refs().is_str() {
1725-
let msg = "remove this method call";
1726-
let mut fallback_span = true;
1727-
if let SelfSource::MethodCall(expr) = source {
1728-
let call_expr = self.tcx.hir().expect_expr(self.tcx.parent_hir_id(expr.hir_id));
1729-
if let Some(span) = call_expr.span.trim_start(expr.span) {
1730-
err.span_suggestion(span, msg, "", Applicability::MachineApplicable);
1731-
fallback_span = false;
1732-
}
1733-
}
1734-
if fallback_span {
1735-
err.span_label(span, msg);
1736-
}
1737-
} else if let Some(similar_candidate) = similar_candidate {
1724+
if let Some(similar_candidate) = similar_candidate {
17381725
// Don't emit a suggestion if we found an actual method
17391726
// that had unsatisfied trait bounds
17401727
if unsatisfied_predicates.is_empty()

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
// tidy-alphabetical-start
9494
#![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))]
9595
#![cfg_attr(not(no_global_oom_handling), feature(const_btree_len))]
96+
#![cfg_attr(test, feature(str_as_str))]
9697
#![feature(alloc_layout_extra)]
9798
#![feature(allocator_api)]
9899
#![feature(array_chunks)]

library/alloc/src/rc/tests.rs

+4
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,11 @@ fn test_from_box_str() {
448448
use std::string::String;
449449

450450
let s = String::from("foo").into_boxed_str();
451+
assert_eq!((&&&s).as_str(), "foo");
452+
451453
let r: Rc<str> = Rc::from(s);
454+
assert_eq!((&r).as_str(), "foo");
455+
assert_eq!(r.as_str(), "foo");
452456

453457
assert_eq!(&r[..], "foo");
454458
}

library/core/src/str/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2736,6 +2736,17 @@ impl str {
27362736
pub fn substr_range(&self, substr: &str) -> Option<Range<usize>> {
27372737
self.as_bytes().subslice_range(substr.as_bytes())
27382738
}
2739+
2740+
/// Returns the same string as a string slice `&str`.
2741+
///
2742+
/// This method is redundant when used directly on `&str`, but
2743+
/// it helps dereferencing other string-like types to string slices,
2744+
/// for example references to `Box<str>` or `Arc<str>`.
2745+
#[inline]
2746+
#[unstable(feature = "str_as_str", issue = "130366")]
2747+
pub fn as_str(&self) -> &str {
2748+
self
2749+
}
27392750
}
27402751

27412752
#[stable(feature = "rust1", since = "1.0.0")]

tests/ui/suggestions/remove-as_str.rs

-21
This file was deleted.

tests/ui/suggestions/remove-as_str.stderr

-27
This file was deleted.

0 commit comments

Comments
 (0)