diff options
| author | bors <bors@rust-lang.org> | 2014-12-08 02:32:31 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-12-08 02:32:31 +0000 |
| commit | 83a44c7fa676b4e5e546ce3d4624e585f9a1e899 (patch) | |
| tree | 36d7db1d2567d86816d4ac6a1ec86276974dbc65 /src/libsyntax/codemap.rs | |
| parent | 8bca470c5acf13aa20022a2c462a89f72de721fc (diff) | |
| parent | 1fea900de7f11d665086141806246842c03b9fc5 (diff) | |
| download | rust-83a44c7fa676b4e5e546ce3d4624e585f9a1e899.tar.gz rust-83a44c7fa676b4e5e546ce3d4624e585f9a1e899.zip | |
auto merge of #19378 : japaric/rust/no-as-slice, r=alexcrichton
Now that we have an overloaded comparison (`==`) operator, and that `Vec`/`String` deref to `[T]`/`str` on method calls, many `as_slice()`/`as_mut_slice()`/`to_string()` calls have become redundant. This patch removes them. These were the most common patterns: - `assert_eq(test_output.as_slice(), "ground truth")` -> `assert_eq(test_output, "ground truth")` - `assert_eq(test_output, "ground truth".to_string())` -> `assert_eq(test_output, "ground truth")` - `vec.as_mut_slice().sort()` -> `vec.sort()` - `vec.as_slice().slice(from, to)` -> `vec.slice(from_to)` --- Note that e.g. `a_string.push_str(b_string.as_slice())` has been left untouched in this PR, since we first need to settle down whether we want to favor the `&*b_string` or the `b_string[]` notation. This is rebased on top of #19167 cc @alexcrichton @aturon
Diffstat (limited to 'src/libsyntax/codemap.rs')
| -rw-r--r-- | src/libsyntax/codemap.rs | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 27e8c265e5c..6bcf562204b 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -290,7 +290,7 @@ impl FileMap { lines.get(line_number).map(|&line| { let begin: BytePos = line - self.start_pos; let begin = begin.to_uint(); - let slice = self.src.as_slice().slice_from(begin); + let slice = self.src.slice_from(begin); match slice.find('\n') { Some(e) => slice.slice_to(e), None => slice @@ -308,8 +308,8 @@ impl FileMap { } pub fn is_real_file(&self) -> bool { - !(self.name.as_slice().starts_with("<") && - self.name.as_slice().ends_with(">")) + !(self.name.starts_with("<") && + self.name.ends_with(">")) } } @@ -336,8 +336,8 @@ impl CodeMap { // Remove utf-8 BOM if any. // FIXME #12884: no efficient/safe way to remove from the start of a string // and reuse the allocation. - let mut src = if src.as_slice().starts_with("\ufeff") { - String::from_str(src.as_slice().slice_from(3)) + let mut src = if src.starts_with("\ufeff") { + String::from_str(src.slice_from(3)) } else { String::from_str(src.as_slice()) }; @@ -346,7 +346,7 @@ impl CodeMap { // This is a workaround to prevent CodeMap.lookup_filemap_idx from accidentally // overflowing into the next filemap in case the last byte of span is also the last // byte of filemap, which leads to incorrect results from CodeMap.span_to_*. - if src.len() > 0 && !src.as_slice().ends_with("\n") { + if src.len() > 0 && !src.ends_with("\n") { src.push('\n'); } @@ -426,14 +426,14 @@ impl CodeMap { if begin.fm.start_pos != end.fm.start_pos { None } else { - Some(begin.fm.src.as_slice().slice(begin.pos.to_uint(), - end.pos.to_uint()).to_string()) + Some(begin.fm.src.slice(begin.pos.to_uint(), + end.pos.to_uint()).to_string()) } } pub fn get_filemap(&self, filename: &str) -> Rc<FileMap> { for fm in self.files.borrow().iter() { - if filename == fm.name.as_slice() { + if filename == fm.name { return fm.clone(); } } @@ -614,11 +614,11 @@ mod test { let cm = init_code_map(); let fmabp1 = cm.lookup_byte_offset(BytePos(22)); - assert_eq!(fmabp1.fm.name, "blork.rs".to_string()); + assert_eq!(fmabp1.fm.name, "blork.rs"); assert_eq!(fmabp1.pos, BytePos(22)); let fmabp2 = cm.lookup_byte_offset(BytePos(24)); - assert_eq!(fmabp2.fm.name, "blork2.rs".to_string()); + assert_eq!(fmabp2.fm.name, "blork2.rs"); assert_eq!(fmabp2.pos, BytePos(0)); } @@ -640,12 +640,12 @@ mod test { let cm = init_code_map(); let loc1 = cm.lookup_char_pos(BytePos(22)); - assert_eq!(loc1.file.name, "blork.rs".to_string()); + assert_eq!(loc1.file.name, "blork.rs"); assert_eq!(loc1.line, 2); assert_eq!(loc1.col, CharPos(10)); let loc2 = cm.lookup_char_pos(BytePos(24)); - assert_eq!(loc2.file.name, "blork2.rs".to_string()); + assert_eq!(loc2.file.name, "blork2.rs"); assert_eq!(loc2.line, 1); assert_eq!(loc2.col, CharPos(0)); } @@ -701,7 +701,7 @@ mod test { let span = Span {lo: BytePos(12), hi: BytePos(23), expn_id: NO_EXPANSION}; let file_lines = cm.span_to_lines(span); - assert_eq!(file_lines.file.name, "blork.rs".to_string()); + assert_eq!(file_lines.file.name, "blork.rs"); assert_eq!(file_lines.lines.len(), 1); assert_eq!(file_lines.lines[0], 1u); } @@ -723,6 +723,6 @@ mod test { let span = Span {lo: BytePos(12), hi: BytePos(23), expn_id: NO_EXPANSION}; let sstr = cm.span_to_string(span); - assert_eq!(sstr, "blork.rs:2:1: 2:12".to_string()); + assert_eq!(sstr, "blork.rs:2:1: 2:12"); } } |
