2019-05-17 16:53:54 -05:00
|
|
|
use crate::utils::{snippet_with_applicability, span_lint, span_lint_and_sugg};
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
|
2019-04-08 15:43:55 -05:00
|
|
|
use rustc::{declare_lint_pass, declare_tool_lint};
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc_errors::Applicability;
|
2018-11-20 07:06:29 -06:00
|
|
|
use std::borrow::Cow;
|
2018-12-29 09:04:45 -06:00
|
|
|
use syntax::ast::*;
|
|
|
|
use syntax::parse::{parser, token};
|
2019-02-17 23:53:30 -06:00
|
|
|
use syntax::tokenstream::{TokenStream, TokenTree};
|
2019-05-17 16:53:54 -05:00
|
|
|
use syntax_pos::symbol::Symbol;
|
2018-04-04 22:46:39 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** This lint warns when you use `println!("")` to
|
|
|
|
/// print a newline.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** You should use `println!()`, which is simpler.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// println!("");
|
|
|
|
/// ```
|
2018-04-04 22:46:39 -05:00
|
|
|
pub PRINTLN_EMPTY_STRING,
|
|
|
|
style,
|
|
|
|
"using `println!(\"\")` with an empty string"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** This lint warns when you use `print!()` with a format
|
|
|
|
/// string that
|
|
|
|
/// ends in a newline.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** You should use `println!()` instead, which appends the
|
|
|
|
/// newline.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-03-09 01:51:23 -06:00
|
|
|
/// ```rust
|
|
|
|
/// # let name = "World";
|
2019-03-05 10:50:33 -06:00
|
|
|
/// print!("Hello {}!\n", name);
|
|
|
|
/// ```
|
|
|
|
/// use println!() instead
|
2019-03-09 01:51:23 -06:00
|
|
|
/// ```rust
|
|
|
|
/// # let name = "World";
|
2019-03-05 10:50:33 -06:00
|
|
|
/// println!("Hello {}!", name);
|
|
|
|
/// ```
|
2018-04-04 22:46:39 -05:00
|
|
|
pub PRINT_WITH_NEWLINE,
|
|
|
|
style,
|
2018-08-07 15:01:10 -05:00
|
|
|
"using `print!()` with a format string that ends in a single newline"
|
2018-04-04 22:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** Checks for printing on *stdout*. The purpose of this lint
|
|
|
|
/// is to catch debugging remnants.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** People often print on *stdout* while debugging an
|
|
|
|
/// application and might forget to remove those prints afterward.
|
|
|
|
///
|
|
|
|
/// **Known problems:** Only catches `print!` and `println!` calls.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// println!("Hello world!");
|
|
|
|
/// ```
|
2018-04-04 22:46:39 -05:00
|
|
|
pub PRINT_STDOUT,
|
|
|
|
restriction,
|
|
|
|
"printing on stdout"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** Checks for use of `Debug` formatting. The purpose of this
|
|
|
|
/// lint is to catch debugging remnants.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** The purpose of the `Debug` trait is to facilitate
|
|
|
|
/// debugging Rust code. It should not be used in in user-facing output.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// println!("{:?}", foo);
|
|
|
|
/// ```
|
2018-04-04 22:46:39 -05:00
|
|
|
pub USE_DEBUG,
|
|
|
|
restriction,
|
|
|
|
"use of `Debug`-based formatting"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** This lint warns about the use of literals as `print!`/`println!` args.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Using literals as `println!` args is inefficient
|
|
|
|
/// (c.f., https://github.com/matthiaskrgr/rust-str-bench) and unnecessary
|
|
|
|
/// (i.e., just put the literal in the format string)
|
|
|
|
///
|
|
|
|
/// **Known problems:** Will also warn with macro calls as arguments that expand to literals
|
|
|
|
/// -- e.g., `println!("{}", env!("FOO"))`.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// println!("{}", "foo");
|
|
|
|
/// ```
|
|
|
|
/// use the literal without formatting:
|
|
|
|
/// ```rust
|
|
|
|
/// println!("foo");
|
|
|
|
/// ```
|
2018-04-04 22:46:39 -05:00
|
|
|
pub PRINT_LITERAL,
|
|
|
|
style,
|
|
|
|
"printing a literal with a format string"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** This lint warns when you use `writeln!(buf, "")` to
|
|
|
|
/// print a newline.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** You should use `writeln!(buf)`, which is simpler.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-03-09 01:51:23 -06:00
|
|
|
/// ```rust
|
|
|
|
/// # use std::fmt::Write;
|
|
|
|
/// # let mut buf = String::new();
|
2019-03-05 16:23:50 -06:00
|
|
|
/// writeln!(buf, "");
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2018-04-04 22:46:39 -05:00
|
|
|
pub WRITELN_EMPTY_STRING,
|
|
|
|
style,
|
2019-03-05 16:23:50 -06:00
|
|
|
"using `writeln!(buf, \"\")` with an empty string"
|
2018-04-04 22:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** This lint warns when you use `write!()` with a format
|
|
|
|
/// string that
|
|
|
|
/// ends in a newline.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** You should use `writeln!()` instead, which appends the
|
|
|
|
/// newline.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-03-09 01:51:23 -06:00
|
|
|
/// ```rust
|
|
|
|
/// # use std::fmt::Write;
|
|
|
|
/// # let mut buf = String::new();
|
|
|
|
/// # let name = "World";
|
2019-03-05 10:50:33 -06:00
|
|
|
/// write!(buf, "Hello {}!\n", name);
|
|
|
|
/// ```
|
2018-04-04 22:46:39 -05:00
|
|
|
pub WRITE_WITH_NEWLINE,
|
|
|
|
style,
|
2018-08-07 15:01:10 -05:00
|
|
|
"using `write!()` with a format string that ends in a single newline"
|
2018-04-04 22:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** This lint warns about the use of literals as `write!`/`writeln!` args.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Using literals as `writeln!` args is inefficient
|
|
|
|
/// (c.f., https://github.com/matthiaskrgr/rust-str-bench) and unnecessary
|
|
|
|
/// (i.e., just put the literal in the format string)
|
|
|
|
///
|
|
|
|
/// **Known problems:** Will also warn with macro calls as arguments that expand to literals
|
|
|
|
/// -- e.g., `writeln!(buf, "{}", env!("FOO"))`.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-03-09 01:51:23 -06:00
|
|
|
/// ```rust
|
|
|
|
/// # use std::fmt::Write;
|
|
|
|
/// # let mut buf = String::new();
|
2019-03-05 10:50:33 -06:00
|
|
|
/// writeln!(buf, "{}", "foo");
|
|
|
|
/// ```
|
2018-04-04 22:46:39 -05:00
|
|
|
pub WRITE_LITERAL,
|
|
|
|
style,
|
|
|
|
"writing a literal with a format string"
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
declare_lint_pass!(Write => [
|
|
|
|
PRINT_WITH_NEWLINE,
|
|
|
|
PRINTLN_EMPTY_STRING,
|
|
|
|
PRINT_STDOUT,
|
|
|
|
USE_DEBUG,
|
|
|
|
PRINT_LITERAL,
|
|
|
|
WRITE_WITH_NEWLINE,
|
|
|
|
WRITELN_EMPTY_STRING,
|
|
|
|
WRITE_LITERAL
|
|
|
|
]);
|
2018-04-04 22:46:39 -05:00
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
impl EarlyLintPass for Write {
|
2018-07-23 06:01:12 -05:00
|
|
|
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &Mac) {
|
2019-05-17 16:53:54 -05:00
|
|
|
if mac.node.path == sym!(println) {
|
2018-07-22 17:19:07 -05:00
|
|
|
span_lint(cx, PRINT_STDOUT, mac.span, "use of `println!`");
|
2018-08-16 11:20:06 -05:00
|
|
|
if let Some(fmtstr) = check_tts(cx, &mac.node.tts, false).0 {
|
2018-07-22 17:19:07 -05:00
|
|
|
if fmtstr == "" {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
PRINTLN_EMPTY_STRING,
|
|
|
|
mac.span,
|
|
|
|
"using `println!(\"\")`",
|
|
|
|
"replace it with",
|
|
|
|
"println!()".to_string(),
|
2018-11-27 08:13:57 -06:00
|
|
|
Applicability::MachineApplicable,
|
2018-07-22 17:19:07 -05:00
|
|
|
);
|
2018-04-04 22:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 16:53:54 -05:00
|
|
|
} else if mac.node.path == sym!(print) {
|
2018-07-22 17:19:07 -05:00
|
|
|
span_lint(cx, PRINT_STDOUT, mac.span, "use of `print!`");
|
2019-02-17 23:53:30 -06:00
|
|
|
if let (Some(fmtstr), _, is_raw) = check_tts(cx, &mac.node.tts, false) {
|
2019-02-18 10:39:23 -06:00
|
|
|
if check_newlines(&fmtstr, is_raw) {
|
2018-09-06 05:33:00 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
PRINT_WITH_NEWLINE,
|
|
|
|
mac.span,
|
|
|
|
"using `print!()` with a format string that ends in a \
|
|
|
|
single newline, consider using `println!()` instead",
|
|
|
|
);
|
2018-04-04 22:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 16:53:54 -05:00
|
|
|
} else if mac.node.path == sym!(write) {
|
2019-02-17 23:53:30 -06:00
|
|
|
if let (Some(fmtstr), _, is_raw) = check_tts(cx, &mac.node.tts, true) {
|
2019-02-18 10:39:23 -06:00
|
|
|
if check_newlines(&fmtstr, is_raw) {
|
2018-09-06 05:33:00 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
WRITE_WITH_NEWLINE,
|
|
|
|
mac.span,
|
|
|
|
"using `write!()` with a format string that ends in a \
|
|
|
|
single newline, consider using `writeln!()` instead",
|
|
|
|
);
|
2018-04-04 22:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 16:53:54 -05:00
|
|
|
} else if mac.node.path == sym!(writeln) {
|
2018-08-16 11:20:06 -05:00
|
|
|
let check_tts = check_tts(cx, &mac.node.tts, true);
|
|
|
|
if let Some(fmtstr) = check_tts.0 {
|
2018-07-22 17:19:07 -05:00
|
|
|
if fmtstr == "" {
|
2018-11-27 08:13:57 -06:00
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
let suggestion = check_tts.1.map_or_else(
|
|
|
|
move || {
|
|
|
|
applicability = Applicability::HasPlaceholders;
|
|
|
|
Cow::Borrowed("v")
|
|
|
|
},
|
|
|
|
move |expr| snippet_with_applicability(cx, expr.span, "v", &mut applicability),
|
|
|
|
);
|
2018-08-20 07:03:13 -05:00
|
|
|
|
2018-07-22 17:19:07 -05:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
WRITELN_EMPTY_STRING,
|
|
|
|
mac.span,
|
2018-08-20 08:50:15 -05:00
|
|
|
format!("using `writeln!({}, \"\")`", suggestion).as_str(),
|
2018-07-22 17:19:07 -05:00
|
|
|
"replace it with",
|
2018-08-20 08:33:43 -05:00
|
|
|
format!("writeln!({})", suggestion),
|
2018-11-27 08:13:57 -06:00
|
|
|
applicability,
|
2018-07-22 17:19:07 -05:00
|
|
|
);
|
2018-04-04 22:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 08:11:13 -06:00
|
|
|
/// Checks the arguments of `print[ln]!` and `write[ln]!` calls. It will return a tuple of two
|
2019-02-17 23:53:30 -06:00
|
|
|
/// options and a bool. The first part of the tuple is `format_str` of the macros. The second part
|
|
|
|
/// of the tuple is in the `write[ln]!` case the expression the `format_str` should be written to.
|
|
|
|
/// The final part is a boolean flag indicating if the string is a raw string.
|
2018-11-27 08:11:13 -06:00
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// Calling this function on
|
2019-03-09 01:51:23 -06:00
|
|
|
/// ```rust
|
|
|
|
/// # use std::fmt::Write;
|
|
|
|
/// # let mut buf = String::new();
|
|
|
|
/// # let something = "something";
|
|
|
|
/// writeln!(buf, "string to write: {}", something);
|
2018-11-27 08:11:13 -06:00
|
|
|
/// ```
|
|
|
|
/// will return
|
|
|
|
/// ```rust,ignore
|
2019-02-17 23:53:30 -06:00
|
|
|
/// (Some("string to write: {}"), Some(buf), false)
|
2018-11-27 08:11:13 -06:00
|
|
|
/// ```
|
2019-02-17 23:53:30 -06:00
|
|
|
fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &TokenStream, is_write: bool) -> (Option<String>, Option<Expr>, bool) {
|
2018-12-29 09:04:45 -06:00
|
|
|
use fmt_macros::*;
|
2019-01-20 02:14:23 -06:00
|
|
|
let tts = tts.clone();
|
2019-02-17 23:53:30 -06:00
|
|
|
let mut is_raw = false;
|
|
|
|
if let TokenStream(Some(tokens)) = &tts {
|
|
|
|
for token in tokens.iter() {
|
2019-05-23 13:19:00 -05:00
|
|
|
if let (TokenTree::Token(_, token::Token::Literal(lit)), _) = token {
|
|
|
|
match lit.kind {
|
|
|
|
token::Str => break,
|
|
|
|
token::StrRaw(_) => {
|
2019-02-17 23:53:30 -06:00
|
|
|
is_raw = true;
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-25 02:45:07 -05:00
|
|
|
let mut parser = parser::Parser::new(&cx.sess.parse_sess, tts, None, false, false, None);
|
2018-08-20 07:03:13 -05:00
|
|
|
let mut expr: Option<Expr> = None;
|
|
|
|
if is_write {
|
2018-08-20 08:33:43 -05:00
|
|
|
expr = match parser.parse_expr().map_err(|mut err| err.cancel()) {
|
|
|
|
Ok(p) => Some(p.into_inner()),
|
2019-02-17 23:53:30 -06:00
|
|
|
Err(_) => return (None, None, is_raw),
|
2018-08-20 07:03:13 -05:00
|
|
|
};
|
|
|
|
// might be `writeln!(foo)`
|
2018-08-20 08:33:43 -05:00
|
|
|
if parser.expect(&token::Comma).map_err(|mut err| err.cancel()).is_err() {
|
2019-02-17 23:53:30 -06:00
|
|
|
return (None, expr, is_raw);
|
2018-08-20 07:03:13 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-16 11:20:06 -05:00
|
|
|
|
2018-08-20 08:33:43 -05:00
|
|
|
let fmtstr = match parser.parse_str().map_err(|mut err| err.cancel()) {
|
|
|
|
Ok(token) => token.0.to_string(),
|
2019-02-17 23:53:30 -06:00
|
|
|
Err(_) => return (None, expr, is_raw),
|
2018-08-20 07:03:13 -05:00
|
|
|
};
|
2018-07-22 17:19:07 -05:00
|
|
|
let tmp = fmtstr.clone();
|
|
|
|
let mut args = vec![];
|
2018-12-27 04:19:20 -06:00
|
|
|
let mut fmt_parser = Parser::new(&tmp, None, Vec::new(), false);
|
2018-07-22 17:19:07 -05:00
|
|
|
while let Some(piece) = fmt_parser.next() {
|
|
|
|
if !fmt_parser.errors.is_empty() {
|
2019-02-17 23:53:30 -06:00
|
|
|
return (None, expr, is_raw);
|
2018-04-04 22:46:39 -05:00
|
|
|
}
|
2018-07-22 17:19:07 -05:00
|
|
|
if let Piece::NextArgument(arg) = piece {
|
|
|
|
if arg.format.ty == "?" {
|
|
|
|
// FIXME: modify rustc's fmt string parser to give us the current span
|
|
|
|
span_lint(cx, USE_DEBUG, parser.prev_span, "use of `Debug`-based formatting");
|
2018-04-04 22:46:39 -05:00
|
|
|
}
|
2018-07-22 17:19:07 -05:00
|
|
|
args.push(arg);
|
2018-04-04 22:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
2018-09-06 05:33:00 -05:00
|
|
|
let lint = if is_write { WRITE_LITERAL } else { PRINT_LITERAL };
|
2018-07-22 17:19:07 -05:00
|
|
|
let mut idx = 0;
|
|
|
|
loop {
|
2018-07-23 06:01:12 -05:00
|
|
|
const SIMPLE: FormatSpec<'_> = FormatSpec {
|
2018-07-22 17:19:07 -05:00
|
|
|
fill: None,
|
|
|
|
align: AlignUnknown,
|
|
|
|
flags: 0,
|
|
|
|
precision: CountImplied,
|
|
|
|
width: CountImplied,
|
|
|
|
ty: "",
|
|
|
|
};
|
2018-10-07 19:08:20 -05:00
|
|
|
if !parser.eat(&token::Comma) {
|
2019-02-17 23:53:30 -06:00
|
|
|
return (Some(fmtstr), expr, is_raw);
|
2018-10-07 19:08:20 -05:00
|
|
|
}
|
|
|
|
let token_expr = match parser.parse_expr().map_err(|mut err| err.cancel()) {
|
|
|
|
Ok(expr) => expr,
|
2019-02-17 23:53:30 -06:00
|
|
|
Err(_) => return (Some(fmtstr), None, is_raw),
|
2018-10-07 19:08:20 -05:00
|
|
|
};
|
2018-08-20 07:03:13 -05:00
|
|
|
match &token_expr.node {
|
2018-07-22 17:19:07 -05:00
|
|
|
ExprKind::Lit(_) => {
|
|
|
|
let mut all_simple = true;
|
|
|
|
let mut seen = false;
|
|
|
|
for arg in &args {
|
|
|
|
match arg.position {
|
2018-11-27 14:14:15 -06:00
|
|
|
ArgumentImplicitlyIs(n) | ArgumentIs(n) => {
|
|
|
|
if n == idx {
|
|
|
|
all_simple &= arg.format == SIMPLE;
|
|
|
|
seen = true;
|
|
|
|
}
|
2018-07-22 17:19:07 -05:00
|
|
|
},
|
|
|
|
ArgumentNamed(_) => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if all_simple && seen {
|
2018-08-20 07:03:13 -05:00
|
|
|
span_lint(cx, lint, token_expr.span, "literal with an empty format string");
|
2018-07-22 17:19:07 -05:00
|
|
|
}
|
|
|
|
idx += 1;
|
|
|
|
},
|
|
|
|
ExprKind::Assign(lhs, rhs) => {
|
2018-07-25 16:31:17 -05:00
|
|
|
if let ExprKind::Lit(_) = rhs.node {
|
|
|
|
if let ExprKind::Path(_, p) = &lhs.node {
|
|
|
|
let mut all_simple = true;
|
|
|
|
let mut seen = false;
|
|
|
|
for arg in &args {
|
|
|
|
match arg.position {
|
2018-09-06 05:33:00 -05:00
|
|
|
ArgumentImplicitlyIs(_) | ArgumentIs(_) => {},
|
2018-11-27 14:14:15 -06:00
|
|
|
ArgumentNamed(name) => {
|
2019-05-13 18:34:08 -05:00
|
|
|
if *p == Symbol::intern(name) {
|
2018-11-27 14:14:15 -06:00
|
|
|
seen = true;
|
|
|
|
all_simple &= arg.format == SIMPLE;
|
|
|
|
}
|
2018-07-25 16:31:17 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if all_simple && seen {
|
|
|
|
span_lint(cx, lint, rhs.span, "literal with an empty format string");
|
2018-07-22 17:19:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => idx += 1,
|
2018-04-21 13:24:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-11 00:28:25 -06:00
|
|
|
|
|
|
|
// Checks if `s` constains a single newline that terminates it
|
2019-02-18 10:39:23 -06:00
|
|
|
// Literal and escaped newlines are both checked (only literal for raw strings)
|
|
|
|
fn check_newlines(s: &str, is_raw: bool) -> bool {
|
|
|
|
if s.ends_with('\n') {
|
|
|
|
return true;
|
|
|
|
} else if is_raw {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-11 00:28:25 -06:00
|
|
|
if s.len() < 2 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let bytes = s.as_bytes();
|
|
|
|
if bytes[bytes.len() - 2] != b'\\' || bytes[bytes.len() - 1] != b'n' {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut escaping = false;
|
|
|
|
for (index, &byte) in bytes.iter().enumerate() {
|
|
|
|
if escaping {
|
|
|
|
if byte == b'n' {
|
|
|
|
return index == bytes.len() - 1;
|
|
|
|
}
|
|
|
|
escaping = false;
|
|
|
|
} else if byte == b'\\' {
|
|
|
|
escaping = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|